1#!/usr/bin/env vpython3 2 3# Copyright 2021 The Chromium Authors 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7import os 8import tempfile 9import unittest 10 11from pyfakefs import fake_filesystem_unittest 12 13from generate_script import _parse_args 14from generate_script import _generate_script 15 16 17class Tests(fake_filesystem_unittest.TestCase): 18 def test_parse_args(self): 19 raw_args = [ 20 '--script-path=./bin/run_foobar', '--exe-dir=.', 21 '--rust-test-executables=metadata.json' 22 ] 23 parsed_args = _parse_args(raw_args) 24 self.assertEqual('./bin/run_foobar', parsed_args.script_path) 25 self.assertEqual('.', parsed_args.exe_dir) 26 self.assertEqual('metadata.json', parsed_args.rust_test_executables) 27 28 def test_generate_script(self): 29 lib_dir = os.path.dirname(__file__) 30 out_dir = os.path.join(lib_dir, '../../../out/rust') 31 args = type('', (), {})() 32 args.make_bat = False 33 args.script_path = os.path.join(out_dir, 'bin/run_foo_bar') 34 args.exe_dir = out_dir 35 36 # pylint: disable=unexpected-keyword-arg 37 with tempfile.NamedTemporaryFile(delete=False, 38 mode='w', 39 encoding='utf-8') as f: 40 filepath = f.name 41 f.write('foo\n') 42 f.write('bar\n') 43 try: 44 args.rust_test_executables = filepath 45 actual = _generate_script(args, 46 should_validate_if_exes_exist=False) 47 finally: 48 os.remove(filepath) 49 50 expected = """ 51#!/bin/bash 52env vpython3 \ 53"$(dirname $0)/../../../testing/scripts/rust/rust_main_program.py" \\ 54 "--rust-test-executable=$(dirname $0)/../bar" \\ 55 "--rust-test-executable=$(dirname $0)/../foo" \\ 56 "$@" 57""".strip() 58 59 self.assertEqual(expected, actual) 60 61 def test_generate_bat(self): 62 lib_dir = os.path.dirname(__file__) 63 out_dir = os.path.join(lib_dir, '../../../out/rust') 64 args = type('', (), {})() 65 args.make_bat = True 66 args.script_path = os.path.join(out_dir, 'bin/run_foo_bar') 67 args.exe_dir = out_dir 68 69 # pylint: disable=unexpected-keyword-arg 70 with tempfile.NamedTemporaryFile(delete=False, 71 mode='w', 72 encoding='utf-8') as f: 73 filepath = f.name 74 f.write('foo\n') 75 f.write('bar\n') 76 try: 77 args.rust_test_executables = filepath 78 actual = _generate_script(args, 79 should_validate_if_exes_exist=False) 80 finally: 81 os.remove(filepath) 82 83 expected = """ 84@echo off 85vpython3 "%~dp0\\../../../testing/scripts/rust\\rust_main_program.py" ^ 86 "--rust-test-executable=%~dp0\\..\\bar.exe" ^ 87 "--rust-test-executable=%~dp0\\..\\foo.exe" ^ 88 %* 89""".strip() 90 91 self.assertEqual(expected, actual) 92 93 94if __name__ == '__main__': 95 unittest.main() 96