forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1257516 - Add a unit test for check_prog(). r=ted
At the same time, shell quote the result it prints if it needs to be.
- Loading branch information
Showing
4 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
python/mozbuild/mozbuild/test/configure/test_checks_configure.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
from __future__ import absolute_import, print_function, unicode_literals | ||
|
||
from StringIO import StringIO | ||
import os | ||
import unittest | ||
|
||
from mozunit import main | ||
|
||
from mozbuild.configure import ( | ||
ConfigureError, | ||
ConfigureSandbox, | ||
) | ||
|
||
from buildconfig import topsrcdir | ||
|
||
|
||
class FindProgramSandbox(ConfigureSandbox): | ||
def __init__(self, *args, **kwargs): | ||
super(FindProgramSandbox, self).__init__(*args, **kwargs) | ||
|
||
# We could define self.find_program_impl and have it automatically | ||
# declared, but then it wouldn't be available in the tested templates. | ||
# We also need to use super().__setitem__ because ours would do | ||
# nothing. | ||
super(FindProgramSandbox, self).__setitem__( | ||
'find_program', self.template_impl(self.find_program)) | ||
|
||
PROGRAMS = { | ||
'known-a': '/usr/bin/known-a', | ||
'known-b': '/usr/local/bin/known-b', | ||
'known c': '/home/user/bin/known c', | ||
} | ||
|
||
for p in PROGRAMS.values(): | ||
PROGRAMS[p] = p | ||
|
||
@staticmethod | ||
def find_program(prog): | ||
return FindProgramSandbox.PROGRAMS.get(prog) | ||
|
||
def __setitem__(self, key, value): | ||
# Avoid util.configure overwriting our mock find_program | ||
if key == 'find_program': | ||
return | ||
|
||
super(FindProgramSandbox, self).__setitem__(key, value) | ||
|
||
|
||
class TestChecksConfigure(unittest.TestCase): | ||
def get_result(self, command='', args=[], environ={}, | ||
prog='/bin/configure'): | ||
config = {} | ||
out = StringIO() | ||
sandbox = FindProgramSandbox(config, environ, [prog] + args, out, out) | ||
base_dir = os.path.join(topsrcdir, 'build', 'moz.configure') | ||
sandbox.exec_file(os.path.join(base_dir, 'util.configure')) | ||
sandbox.exec_file(os.path.join(base_dir, 'checks.configure')) | ||
|
||
status = 0 | ||
try: | ||
exec(command, sandbox) | ||
except SystemExit as e: | ||
status = e.code | ||
|
||
return config, out.getvalue(), status | ||
|
||
def test_check_prog(self): | ||
config, out, status = self.get_result( | ||
'check_prog("FOO", ("known-a",))') | ||
self.assertEqual(status, 0) | ||
self.assertEqual(config, {'FOO': '/usr/bin/known-a'}) | ||
self.assertEqual(out, 'checking for foo... /usr/bin/known-a\n') | ||
|
||
config, out, status = self.get_result( | ||
'check_prog("FOO", ("unknown", "known-b", "known c"))') | ||
self.assertEqual(status, 0) | ||
self.assertEqual(config, {'FOO': '/usr/local/bin/known-b'}) | ||
self.assertEqual(out, 'checking for foo... /usr/local/bin/known-b\n') | ||
|
||
config, out, status = self.get_result( | ||
'check_prog("FOO", ("unknown", "unknown-2", "known c"))') | ||
self.assertEqual(status, 0) | ||
self.assertEqual(config, {'FOO': '/home/user/bin/known c'}) | ||
self.assertEqual(out, "checking for foo... '/home/user/bin/known c'\n") | ||
|
||
config, out, status = self.get_result( | ||
'check_prog("FOO", ("unknown",))') | ||
self.assertEqual(status, 1) | ||
self.assertEqual(config, {}) | ||
self.assertEqual(out, 'checking for foo... not found\n' | ||
'ERROR: Cannot find foo (tried: unknown)\n') | ||
|
||
config, out, status = self.get_result( | ||
'check_prog("FOO", ("unknown", "unknown-2", "unknown 3"))') | ||
self.assertEqual(status, 1) | ||
self.assertEqual(config, {}) | ||
self.assertEqual(out, 'checking for foo... not found\n' | ||
'ERROR: Cannot find foo ' | ||
"(tried: unknown, unknown-2, 'unknown 3')\n") | ||
|
||
config, out, status = self.get_result( | ||
'check_prog("FOO", ("unknown", "unknown-2", "unknown 3"), ' | ||
'allow_missing=True)') | ||
self.assertEqual(status, 0) | ||
self.assertEqual(config, {'FOO': ':'}) | ||
self.assertEqual(out, 'checking for foo... not found\n') | ||
|
||
def test_check_prog_with_args(self): | ||
config, out, status = self.get_result( | ||
'check_prog("FOO", ("unknown", "known-b", "known c"))', | ||
['FOO=known-a']) | ||
self.assertEqual(status, 0) | ||
self.assertEqual(config, {'FOO': '/usr/bin/known-a'}) | ||
self.assertEqual(out, 'checking for foo... /usr/bin/known-a\n') | ||
|
||
config, out, status = self.get_result( | ||
'check_prog("FOO", ("unknown", "known-b", "known c"))', | ||
['FOO=/usr/bin/known-a']) | ||
self.assertEqual(status, 0) | ||
self.assertEqual(config, {'FOO': '/usr/bin/known-a'}) | ||
self.assertEqual(out, 'checking for foo... /usr/bin/known-a\n') | ||
|
||
config, out, status = self.get_result( | ||
'check_prog("FOO", ("unknown", "known-b", "known c"))', | ||
['FOO=/usr/local/bin/known-a']) | ||
self.assertEqual(status, 1) | ||
self.assertEqual(config, {}) | ||
self.assertEqual(out, 'checking for foo... not found\n' | ||
'ERROR: Cannot find foo ' | ||
'(tried: /usr/local/bin/known-a)\n') | ||
|
||
config, out, status = self.get_result( | ||
'check_prog("FOO", ("unknown",))', | ||
['FOO=known c']) | ||
self.assertEqual(status, 0) | ||
self.assertEqual(config, {'FOO': '/home/user/bin/known c'}) | ||
self.assertEqual(out, "checking for foo... '/home/user/bin/known c'\n") | ||
|
||
config, out, status = self.get_result( | ||
'check_prog("FOO", ("unknown", "unknown-2", "unknown 3"), ' | ||
'allow_missing=True)', ['FOO=unknown']) | ||
self.assertEqual(status, 1) | ||
self.assertEqual(config, {}) | ||
self.assertEqual(out, 'checking for foo... not found\n' | ||
'ERROR: Cannot find foo (tried: unknown)\n') | ||
|
||
def test_check_prog_configure_error(self): | ||
with self.assertRaises(ConfigureError) as e: | ||
self.get_result('check_prog("FOO", "foo")') | ||
|
||
self.assertEqual(e.exception.message, | ||
'progs should be a list or tuple!') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |