def test_literal(self): self.assertQuote(shell_literal('>'), False, '>', '>') self.assertQuote(shell_literal(''), False, '', '') s = shell_literal('>') + 'foo bar' self.assertEqual(windows.quote(s), '>"foo bar"') self.assertEqual(windows.quote_info(s), ('>"foo bar"', True))
def test_literal(self): self.assertQuote(shell_literal('>'), False, '>', '>') self.assertQuote(shell_literal(''), False, '', '') s = shell_literal('>') + 'foo bar' self.assertEqual(posix.quote(s), ">'foo bar'") self.assertEqual(posix.quote_info(s), (">'foo bar'", True))
def test_single(self): env = {'NAME': 'VALUE'} self.assertEqual( posix.local_env(env, 'cmd'), shell_list([ jbos('NAME', shell_literal('='), 'VALUE'), shell_literal('cmd') ])) self.assertEqual( posix.local_env(env, ['cmd']), shell_list([jbos('NAME', shell_literal('='), 'VALUE'), 'cmd']))
def test_string(self): self.assertEqual(posix.escape_line('foobar'), shell_list([shell_literal(r'foobar')])) with mock.patch('bfg9000.shell.posix.platform_info', return_value=self.FakePlatform('posix')): self.assertEqual(posix.escape_line(r'foo\bar'), shell_list([shell_literal(r'foo\bar')])) with mock.patch('bfg9000.shell.posix.platform_info', return_value=self.FakePlatform('windows')): self.assertEqual(posix.escape_line(r'foo\bar'), shell_list([shell_literal(r'foo\\bar')]))
def test_string(self): class FakePlatform(object): def __init__(self, family): self.family = family with mock.patch('bfg9000.shell.posix.platform_info', return_value=FakePlatform('posix')): self.assertEqual(posix.escape_line(r'foo\bar'), shell_list([shell_literal(r'foo\bar')])) with mock.patch('bfg9000.shell.posix.platform_info', return_value=FakePlatform('windows')): self.assertEqual(posix.escape_line(r'foo\bar'), shell_list([shell_literal(r'foo\\bar')]))
def test_env_vars(self): self._init_setenv() env = {'FOO': 'foo'} self.assertEqual(self.setenv(env, ['echo', 'hi']), [ self.setenv, jbos('FOO', shell_literal('='), 'foo'), '--', 'echo', 'hi' ]) self.assertEqual( self.setenv(env, 'echo hi'), shell_list([ self.setenv, jbos('FOO', shell_literal('='), 'foo'), '--', shell_literal('echo hi') ]))
def test_multiple(self): self.assertEqual(windows.join_lines(['foo', 'bar']), shell_list([ shell_literal('foo'), shell_literal('&&'), shell_literal('bar'), ])) self.assertEqual( windows.join_lines([['foo', 'bar'], 'baz']), shell_list([ 'foo', 'bar', shell_literal('&&'), shell_literal('baz'), ]) )
def test_multiple(self): env = OrderedDict((('FOO', 'oof'), ('BAR', 'rab'))) self.assertEqual( posix.local_env(env, 'cmd'), shell_list([ jbos('FOO', shell_literal('='), 'oof'), jbos('BAR', shell_literal('='), 'rab'), shell_literal('cmd') ])) self.assertEqual( posix.local_env(env, ['cmd']), shell_list([ jbos('FOO', shell_literal('='), 'oof'), jbos('BAR', shell_literal('='), 'rab'), 'cmd' ]))
def test_single(self): env = {'NAME': 'VALUE'} self.assertEqual(windows.global_env(env), shell_list(['set', 'NAME=VALUE'])) self.assertEqual( windows.global_env(env, ['cmd']), shell_list([ 'set', 'NAME=VALUE', shell_literal('&&'), shell_literal('cmd') ])) self.assertEqual( windows.global_env(env, [['cmd']]), shell_list(['set', 'NAME=VALUE', shell_literal('&&'), 'cmd']))
def test_extras(self): test_exe = self.make_extras() cmd, deps = self._build_commands() self.assertEqual(cmd, [[ jbos('VAR', shell_literal('='), 'value'), test_exe, '--foo' ]]) self.assertEqual(deps, [test_exe])
def mock_local_env(env, line): if env: eq = shell_literal('=') env_vars = [jbos(safe_str(name), eq, safe_str(value)) for name, value in env.items()] else: env_vars = [] return env_vars + wshell.escape_line(line, listify=True)
def test_jbos(self): self.assertEqual( posix.escape_line(jbos('foo', literal('bar'))), shell_list([jbos(shell_literal('foo'), literal('bar'))])) s = jbos(r'foo\bar', literal(r'baz\quux')) with mock.patch('bfg9000.shell.posix.platform_info', return_value=self.FakePlatform('posix')): self.assertEqual( posix.escape_line(s), shell_list( [jbos(shell_literal(r'foo\bar'), literal(r'baz\quux'))])) with mock.patch('bfg9000.shell.posix.platform_info', return_value=self.FakePlatform('windows')): self.assertEqual( posix.escape_line(s), shell_list( [jbos(shell_literal(r'foo\\bar'), literal(r'baz\quux'))]))
def test_arguments_shell_list(self): file = file_types.File(Path('foo.c', Root.srcdir)) output = file_types.File(Path('foo.o')) self.compdb.append(file=file, output=output, arguments=shell_list([ 'cmd', shell_literal('<'), file, shell_literal('>>'), output ])) self.assertEqual(self._db_to_json(), [ { 'directory': self.env.builddir.string(), 'command': 'cmd < {} >> {}'.format(file.path.string(self.base_dirs), output.path.string(self.base_dirs)), 'file': file.path.string(self.base_dirs), 'output': 'foo.o' }, ])
def test_single(self): env = {'NAME': 'VALUE'} self.assertEqual( posix.global_env(env), shell_list(['export', jbos('NAME', shell_literal('='), 'VALUE')])) self.assertEqual( posix.global_env(env, ['cmd']), shell_list([ 'export', jbos('NAME', shell_literal('='), 'VALUE'), shell_literal('&&'), shell_literal('cmd') ])) self.assertEqual( posix.global_env(env, [['cmd']]), shell_list([ 'export', jbos('NAME', shell_literal('='), 'VALUE'), shell_literal('&&'), 'cmd' ]))
def test_multiple(self): env = OrderedDict((('FOO', 'oof'), ('BAR', 'rab'))) self.assertEqual(windows.global_env(env), shell_list([ 'set', 'FOO=oof', shell_literal('&&'), 'set', 'BAR=rab' ])) self.assertEqual(windows.global_env(env, ['cmd']), shell_list([ 'set', 'FOO=oof', shell_literal('&&'), 'set', 'BAR=rab', shell_literal('&&'), shell_literal('cmd') ])) self.assertEqual(windows.global_env(env, [['cmd']]), shell_list([ 'set', 'FOO=oof', shell_literal('&&'), 'set', 'BAR=rab', shell_literal('&&'), 'cmd' ]))
def test_dependency(self): out = Writer(StringIO()) out.write(safe_str.shell_literal('foo: $bar|baz,quux'), Syntax.dependency) self.assertEqual(out.stream.getvalue(), 'foo' + esc_colon + '\\ $$bar\\|baz,quux')
def test_empty(self): self.assertEqual(posix.global_env({}), []) self.assertEqual(posix.global_env({}, ['cmd']), shell_list([shell_literal('cmd')])) self.assertEqual(posix.global_env({}, [['cmd']]), ['cmd'])
def test_depfixer(self): self.assertEqual(self.tool('depfile'), shell_list([ self.tool, shell_literal('<'), 'depfile', shell_literal('>>'), 'depfile' ]))
def test_output(self): out = Writer(StringIO()) out.write(safe_str.shell_literal('foo: $bar'), Syntax.output) self.assertEqual(out.stream.getvalue(), 'foo$:$ $$bar')
def test_empty(self): self.assertEqual(posix.local_env({}, 'cmd'), shell_list([shell_literal('cmd')])) self.assertEqual(posix.local_env({}, ['cmd']), ['cmd'])
def test_single(self): self.assertEqual(posix.join_lines(['foo']), shell_list([shell_literal('foo')])) self.assertEqual(posix.join_lines([['foo']]), ['foo']) self.assertEqual(posix.join_lines([['foo', 'bar']]), ['foo', 'bar'])
def test_stringify_equal(self): r = SimpleRequirement('foo', '==1.0') self.assertEqual(safe_str(r), shell_literal('foo = 1.0'))
def test_stringify_no_version(self): r = SimpleRequirement('foo') self.assertEqual(safe_str(r), shell_literal('foo'))
def test_input(self): self.out.write(safe_str.shell_literal('foo: $bar'), Syntax.input) self.assertEqual(self.out.stream.getvalue(), 'foo$:$ $$bar')
def test_clean(self): self.out.write(safe_str.shell_literal('foo: $bar'), Syntax.clean) self.assertEqual(self.out.stream.getvalue(), 'foo: $$bar')
def test_basic(self): self._init_setenv() self.assertEqual(self.setenv({}, ['echo', 'hi']), ['echo', 'hi']) self.assertEqual(self.setenv({}, 'echo hi'), shell_list([shell_literal('echo hi')]))
def test_convert_to_str(self): with self.assertRaises(NotImplementedError): str(safe_str.literal('foo')) with self.assertRaises(NotImplementedError): str(safe_str.shell_literal('foo'))
def test_multiple(self): env = OrderedDict((('FOO', 'oof'), ('BAR', 'rab'))) self.assertEqual( posix.global_env(env), shell_list([ 'export', jbos('FOO', shell_literal('='), 'oof'), shell_literal('&&'), 'export', jbos('BAR', shell_literal('='), 'rab') ])) self.assertEqual( posix.global_env(env, ['cmd']), shell_list([ 'export', jbos('FOO', shell_literal('='), 'oof'), shell_literal('&&'), 'export', jbos('BAR', shell_literal('='), 'rab'), shell_literal('&&'), shell_literal('cmd') ])) self.assertEqual( posix.global_env(env, [['cmd']]), shell_list([ 'export', jbos('FOO', shell_literal('='), 'oof'), shell_literal('&&'), 'export', jbos('BAR', shell_literal('='), 'rab'), shell_literal('&&'), 'cmd' ]))
def test_literal(self): self.assertEqual(posix.join(['foo bar', shell_literal('>'), 'baz']), "'foo bar' > baz") self.assertEqual(posix.join(['foo bar' + shell_literal('>'), 'baz']), "'foo bar'> baz")
def test_clean(self): out = Writer(StringIO()) out.write(safe_str.shell_literal('foo: $bar|baz,quux'), Syntax.clean) self.assertEqual(out.stream.getvalue(), 'foo: $$bar|baz,quux')