3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing,
14 # software distributed under the License is distributed on an
15 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 # KIND, either express or implied. See the License for the
17 # specific language governing permissions and limitations
20 from __future__
import print_function
30 class TestStalenessCheck(unittest
.TestCase
):
32 CURRENT_DIR_PATH
= os
.path
.dirname(os
.path
.realpath(__file__
))
33 THRIFT_EXECUTABLE_PATH
= None
34 SINGLE_THRIFT_FILE_PATH
= os
.path
.join(CURRENT_DIR_PATH
, "Single.thrift")
35 INCLUDING_THRIFT_FILE_PATH
= os
.path
.join(CURRENT_DIR_PATH
, "Including.thrift")
36 INCLUDED_THRIFT_FILE_PATH
= os
.path
.join(CURRENT_DIR_PATH
, "Included.thrift")
38 def test_staleness_check_of_single_thrift_file_without_changed_output(self
):
39 temp_dir
= tempfile
.mkdtemp(dir=TestStalenessCheck
.CURRENT_DIR_PATH
)
41 command
= [TestStalenessCheck
.THRIFT_EXECUTABLE_PATH
, "-gen", "cpp", "-o", temp_dir
]
42 command
+= [TestStalenessCheck
.SINGLE_THRIFT_FILE_PATH
]
43 subprocess
.call(command
)
45 used_file_path
= os
.path
.join(temp_dir
, "gen-cpp", "Single_constants.cpp")
47 first_modification_time
= os
.path
.getmtime(os
.path
.join(used_file_path
))
51 subprocess
.call(command
)
53 second_modification_time
= os
.path
.getmtime(used_file_path
)
55 self
.assertEqual(second_modification_time
, first_modification_time
)
57 shutil
.rmtree(temp_dir
, ignore_errors
=True)
59 def test_staleness_check_of_single_thrift_file_with_changed_output(self
):
60 temp_dir
= tempfile
.mkdtemp(dir=TestStalenessCheck
.CURRENT_DIR_PATH
)
62 command
= [TestStalenessCheck
.THRIFT_EXECUTABLE_PATH
, "-gen", "cpp", "-o", temp_dir
]
63 command
+= [TestStalenessCheck
.SINGLE_THRIFT_FILE_PATH
]
64 subprocess
.call(command
)
66 used_file_path
= os
.path
.join(temp_dir
, "gen-cpp", "Single_constants.cpp")
68 first_modification_time
= os
.path
.getmtime(os
.path
.join(used_file_path
))
69 used_file
= open(used_file_path
, "r")
70 first_contents
= used_file
.read()
73 used_file
= open(used_file_path
, "a")
74 used_file
.write("\n/* This is a comment */\n")
79 subprocess
.call(command
)
81 second_modification_time
= os
.path
.getmtime(used_file_path
)
82 used_file
= open(used_file_path
, "r")
83 second_contents
= used_file
.read()
86 self
.assertGreater(second_modification_time
, first_modification_time
)
87 self
.assertEqual(first_contents
, second_contents
)
89 shutil
.rmtree(temp_dir
, ignore_errors
=True)
91 def test_staleness_check_of_included_file(self
):
92 temp_dir
= tempfile
.mkdtemp(dir=TestStalenessCheck
.CURRENT_DIR_PATH
)
94 temp_included_file_path
= os
.path
.join(temp_dir
, "Included.thrift")
95 temp_including_file_path
= os
.path
.join(temp_dir
, "Including.thrift")
97 shutil
.copy2(TestStalenessCheck
.INCLUDED_THRIFT_FILE_PATH
, temp_included_file_path
)
98 shutil
.copy2(TestStalenessCheck
.INCLUDING_THRIFT_FILE_PATH
, temp_including_file_path
)
100 command
= [TestStalenessCheck
.THRIFT_EXECUTABLE_PATH
, "-gen", "cpp", "-recurse", "-o", temp_dir
]
101 command
+= [temp_including_file_path
]
103 subprocess
.call(command
)
105 included_constants_cpp_file_path
= os
.path
.join(temp_dir
, "gen-cpp", "Included_constants.cpp")
106 including_constants_cpp_file_path
= os
.path
.join(temp_dir
, "gen-cpp", "Including_constants.cpp")
108 included_constants_cpp_first_modification_time
= os
.path
.getmtime(included_constants_cpp_file_path
)
109 including_constants_cpp_first_modification_time
= os
.path
.getmtime(including_constants_cpp_file_path
)
111 temp_included_file
= open(temp_included_file_path
, "a")
112 temp_included_file
.write("\nconst i32 an_integer = 42\n")
113 temp_included_file
.close()
117 subprocess
.call(command
)
119 included_constants_cpp_second_modification_time
= os
.path
.getmtime(included_constants_cpp_file_path
)
120 including_constants_cpp_second_modification_time
= os
.path
.getmtime(including_constants_cpp_file_path
)
123 included_constants_cpp_second_modification_time
, included_constants_cpp_first_modification_time
)
125 including_constants_cpp_first_modification_time
, including_constants_cpp_second_modification_time
)
127 shutil
.rmtree(temp_dir
, ignore_errors
=True)
131 suite
= unittest
.TestSuite()
132 loader
= unittest
.TestLoader()
133 suite
.addTest(loader
.loadTestsFromTestCase(TestStalenessCheck
))
137 if __name__
== "__main__":
138 # The path of Thrift compiler is passed as an argument to the test script.
139 # Remove it to not confuse the unit testing framework
140 TestStalenessCheck
.THRIFT_EXECUTABLE_PATH
= sys
.argv
[-1]
142 unittest
.main(defaultTest
="suite", testRunner
=unittest
.TextTestRunner(verbosity
=2))