3 """Runs ./ninja and checks if the output is correct.
5 In order to simulate a smart terminal it uses the 'script' command.
15 default_env = dict(os.environ)
16 if 'NINJA_STATUS' in default_env:
17 del default_env['NINJA_STATUS']
18 if 'CLICOLOR_FORCE' in default_env:
19 del default_env['CLICOLOR_FORCE']
20 default_env['TERM'] = ''
21 NINJA_PATH = os.path.abspath('./ninja')
23 def run(build_ninja, flags='', pipe=False, env=default_env):
24 with tempfile.TemporaryDirectory() as d:
26 with open('build.ninja', 'w') as f:
29 ninja_cmd = '{} {}'.format(NINJA_PATH, flags)
32 output = subprocess.check_output([ninja_cmd], shell=True, env=env)
33 elif platform.system() == 'Darwin':
34 output = subprocess.check_output(['script', '-q', '/dev/null', 'bash', '-c', ninja_cmd],
37 output = subprocess.check_output(['script', '-qfec', ninja_cmd, '/dev/null'],
39 except subprocess.CalledProcessError as err:
40 sys.stdout.buffer.write(err.output)
43 for line in output.decode('utf-8').splitlines(True):
44 if len(line) > 0 and line[-1] == '\r':
46 final_output += line.replace('\r', '')
49 @unittest.skipIf(platform.system() == 'Windows', 'These test methods do not work on Windows')
50 class Output(unittest.TestCase):
51 def test_issue_1418(self):
54 command = sleep $delay && echo $out
55 description = echo $out
72 def test_issue_1214(self):
73 print_red = '''rule echo
74 command = printf '\x1b[31mred\x1b[0m'
75 description = echo $out
79 # Only strip color when ninja's output is piped.
80 self.assertEqual(run(print_red),
84 self.assertEqual(run(print_red, pipe=True),
88 # Even in verbose mode, colors should still only be stripped when piped.
89 self.assertEqual(run(print_red, flags='-v'),
90 '''[1/1] printf '\x1b[31mred\x1b[0m'
93 self.assertEqual(run(print_red, flags='-v', pipe=True),
94 '''[1/1] printf '\x1b[31mred\x1b[0m'
98 # CLICOLOR_FORCE=1 can be used to disable escape code stripping.
99 env = default_env.copy()
100 env['CLICOLOR_FORCE'] = '1'
101 self.assertEqual(run(print_red, pipe=True, env=env),
106 def test_pr_1685(self):
107 # Running those tools without .ninja_deps and .ninja_log shouldn't fail.
108 self.assertEqual(run('', flags='-t recompact'), '')
109 self.assertEqual(run('', flags='-t restat'), '')
111 def test_status(self):
112 self.assertEqual(run(''), 'ninja: no work to do.\n')
114 if __name__ == '__main__':