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