2 # Copyright (c) 2014 Google, Inc
4 # SPDX-License-Identifier: GPL-2.0+
20 class TestFunctional(unittest.TestCase):
21 """Functional test for buildman.
23 This aims to test from just below the invocation of buildman (parsing
24 of arguments) to 'make' and 'git' invocation. It is not a true
25 emd-to-end test, as it mocks git, make and the tool chain. But this
26 makes it easier to detect when the builder is doing the wrong thing,
27 since in many cases this test code will fail. For example, only a
28 very limited subset of 'git' arguments is supported - anything
32 self._base_dir = tempfile.mkdtemp()
33 self._git_dir = os.path.join(self._base_dir, 'src')
34 self._buildman_pathname = sys.argv[0]
35 self._buildman_dir = os.path.dirname(sys.argv[0])
36 command.test_result = self._HandleCommand
37 self._toolchains = toolchain.Toolchains()
38 self._toolchains.Add('gcc', test=False)
41 shutil.rmtree(self._base_dir)
43 def _RunBuildman(self, *args):
44 return command.RunPipe([[self._buildman_pathname] + list(args)],
45 capture=True, capture_stderr=True)
47 def _RunControl(self, *args):
48 sys.argv = [sys.argv[0]] + list(args)
49 options, args = cmdline.ParseArgs()
50 return control.DoBuildman(options, args, toolchains=self._toolchains,
51 make_func=self._HandleMake)
53 def testFullHelp(self):
54 command.test_result = None
55 result = self._RunBuildman('-H')
56 help_file = os.path.join(self._buildman_dir, 'README')
57 self.assertEqual(len(result.stdout), os.path.getsize(help_file))
58 self.assertEqual(0, len(result.stderr))
59 self.assertEqual(0, result.return_code)
62 command.test_result = None
63 result = self._RunBuildman('-h')
64 help_file = os.path.join(self._buildman_dir, 'README')
65 self.assertTrue(len(result.stdout) > 1000)
66 self.assertEqual(0, len(result.stderr))
67 self.assertEqual(0, result.return_code)
69 def testGitSetup(self):
70 """Test gitutils.Setup(), from outside the module itself"""
71 command.test_result = command.CommandResult(return_code=1)
73 self.assertEqual(gitutil.use_no_decorate, False)
75 command.test_result = command.CommandResult(return_code=0)
77 self.assertEqual(gitutil.use_no_decorate, True)
79 def _HandleCommandGitLog(self, args):
81 return command.CommandResult(return_code=0)
83 # Not handled, so abort
87 def _HandleCommandGit(self, in_args):
88 """Handle execution of a git command
90 This uses a hacked-up parser.
93 in_args: Arguments after 'git' from the command line
95 git_args = [] # Top-level arguments to git itself
96 sub_cmd = None # Git sub-command selected
97 args = [] # Arguments to the git sub-command
105 if sub_cmd == 'config':
106 return command.CommandResult(return_code=0)
107 elif sub_cmd == 'log':
108 return self._HandleCommandGitLog(args)
110 # Not handled, so abort
111 print 'git', git_args, sub_cmd, args
114 def _HandleCommandNm(self, args):
115 return command.CommandResult(return_code=0)
117 def _HandleCommandObjdump(self, args):
118 return command.CommandResult(return_code=0)
120 def _HandleCommandSize(self, args):
121 return command.CommandResult(return_code=0)
123 def _HandleCommand(self, **kwargs):
124 """Handle a command execution.
126 The command is in kwargs['pipe-list'], as a list of pipes, each a
127 list of commands. The command should be emulated as required for
131 A CommandResult object
133 pipe_list = kwargs['pipe_list']
134 if len(pipe_list) != 1:
135 print 'invalid pipe', kwargs
137 cmd = pipe_list[0][0]
138 args = pipe_list[0][1:]
140 return self._HandleCommandGit(args)
141 elif cmd == './scripts/show-gnu-make':
142 return command.CommandResult(return_code=0, stdout='make')
144 return self._HandleCommandNm(args)
145 elif cmd == 'objdump':
146 return self._HandleCommandObjdump(args)
148 return self._HandleCommandSize(args)
150 # Not handled, so abort
151 print 'unknown command', kwargs
153 return command.CommandResult(return_code=0)
155 def _HandleMake(self, commit, brd, stage, cwd, *args, **kwargs):
156 """Handle execution of 'make'
159 commit: Commit object that is being built
160 brd: Board object that is being built
161 stage: Stage that we are at (mrproper, config, build)
162 cwd: Directory where make should be run
163 args: Arguments to pass to make
164 kwargs: Arguments to pass to command.RunPipe()
166 if stage == 'mrproper':
167 return command.CommandResult(return_code=0)
168 elif stage == 'config':
169 return command.CommandResult(return_code=0,
170 combined='Test configuration complete')
171 elif stage == 'build':
172 return command.CommandResult(return_code=0)
174 # Not handled, so abort
178 def testCurrentSource(self):
179 """Very simple test to invoke buildman on the current source"""
181 lines = terminal.GetPrintTestLines()
182 self.assertTrue(lines[0].text.startswith('Building current source'))