Mustafa Senol Cosar | 09c1f37 | 2018-04-04 15:25:28 +0300 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 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 |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 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 |
| 18 | # under the License. |
| 19 | # |
| 20 | from __future__ import print_function |
| 21 | import os |
| 22 | import shutil |
| 23 | import subprocess |
| 24 | import sys |
| 25 | import tempfile |
| 26 | import time |
| 27 | import unittest |
| 28 | |
| 29 | |
| 30 | class TestStalenessCheck(unittest.TestCase): |
| 31 | |
| 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") |
| 37 | |
| 38 | def test_staleness_check_of_single_thrift_file_without_changed_output(self): |
| 39 | temp_dir = tempfile.mkdtemp(dir=TestStalenessCheck.CURRENT_DIR_PATH) |
| 40 | |
| 41 | command = [TestStalenessCheck.THRIFT_EXECUTABLE_PATH, "-gen", "cpp", "-o", temp_dir] |
| 42 | command += [TestStalenessCheck.SINGLE_THRIFT_FILE_PATH] |
| 43 | subprocess.call(command) |
| 44 | |
| 45 | used_file_path = os.path.join(temp_dir, "gen-cpp", "Single_constants.cpp") |
| 46 | |
| 47 | first_modification_time = os.path.getmtime(os.path.join(used_file_path)) |
| 48 | |
| 49 | time.sleep(0.1) |
| 50 | |
| 51 | subprocess.call(command) |
| 52 | |
| 53 | second_modification_time = os.path.getmtime(used_file_path) |
| 54 | |
| 55 | self.assertEqual(second_modification_time, first_modification_time) |
| 56 | |
| 57 | shutil.rmtree(temp_dir, ignore_errors=True) |
| 58 | |
| 59 | def test_staleness_check_of_single_thrift_file_with_changed_output(self): |
| 60 | temp_dir = tempfile.mkdtemp(dir=TestStalenessCheck.CURRENT_DIR_PATH) |
| 61 | |
| 62 | command = [TestStalenessCheck.THRIFT_EXECUTABLE_PATH, "-gen", "cpp", "-o", temp_dir] |
| 63 | command += [TestStalenessCheck.SINGLE_THRIFT_FILE_PATH] |
| 64 | subprocess.call(command) |
| 65 | |
| 66 | used_file_path = os.path.join(temp_dir, "gen-cpp", "Single_constants.cpp") |
| 67 | |
| 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() |
| 71 | used_file.close() |
| 72 | |
| 73 | used_file = open(used_file_path, "a") |
| 74 | used_file.write("\n/* This is a comment */\n") |
| 75 | used_file.close() |
| 76 | |
| 77 | time.sleep(0.1) |
| 78 | |
| 79 | subprocess.call(command) |
| 80 | |
| 81 | second_modification_time = os.path.getmtime(used_file_path) |
| 82 | used_file = open(used_file_path, "r") |
| 83 | second_contents = used_file.read() |
| 84 | used_file.close() |
| 85 | |
| 86 | self.assertGreater(second_modification_time, first_modification_time) |
| 87 | self.assertEqual(first_contents, second_contents) |
| 88 | |
| 89 | shutil.rmtree(temp_dir, ignore_errors=True) |
| 90 | |
| 91 | def test_staleness_check_of_included_file(self): |
| 92 | temp_dir = tempfile.mkdtemp(dir=TestStalenessCheck.CURRENT_DIR_PATH) |
| 93 | |
| 94 | temp_included_file_path = os.path.join(temp_dir, "Included.thrift") |
| 95 | temp_including_file_path = os.path.join(temp_dir, "Including.thrift") |
| 96 | |
| 97 | shutil.copy2(TestStalenessCheck.INCLUDED_THRIFT_FILE_PATH, temp_included_file_path) |
| 98 | shutil.copy2(TestStalenessCheck.INCLUDING_THRIFT_FILE_PATH, temp_including_file_path) |
| 99 | |
| 100 | command = [TestStalenessCheck.THRIFT_EXECUTABLE_PATH, "-gen", "cpp", "-recurse", "-o", temp_dir] |
| 101 | command += [temp_including_file_path] |
| 102 | |
| 103 | subprocess.call(command) |
| 104 | |
| 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") |
| 107 | |
| 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) |
| 110 | |
| 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() |
| 114 | |
| 115 | time.sleep(0.1) |
| 116 | |
| 117 | subprocess.call(command) |
| 118 | |
| 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) |
| 121 | |
| 122 | self.assertGreater( |
| 123 | included_constants_cpp_second_modification_time, included_constants_cpp_first_modification_time) |
| 124 | self.assertEqual( |
| 125 | including_constants_cpp_first_modification_time, including_constants_cpp_second_modification_time) |
| 126 | |
| 127 | shutil.rmtree(temp_dir, ignore_errors=True) |
| 128 | |
| 129 | |
| 130 | def suite(): |
| 131 | suite = unittest.TestSuite() |
| 132 | loader = unittest.TestLoader() |
| 133 | suite.addTest(loader.loadTestsFromTestCase(TestStalenessCheck)) |
| 134 | return suite |
| 135 | |
| 136 | |
| 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] |
| 141 | del sys.argv[-1] |
| 142 | unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) |