def test_expand_word_jbos(self): p = Placeholder('files') zero = AttrDict(files=[]) one = AttrDict(files=[literal('foo')]) two = AttrDict(files=[literal('foo'), literal('bar')]) foo = jbos('{', literal('foo'), '}') bar = jbos('{', literal('bar'), '}') j = '{' + p + '}' self.assertEqual(Placeholder.expand_word(j, zero), []) self.assertEqual(Placeholder.expand_word(j, one), [foo]) self.assertEqual(Placeholder.expand_word(j, two), [foo, bar]) j = '{' + p[0] + '}' with self.assertRaises(IndexError): Placeholder.expand_word(j, zero) self.assertEqual(Placeholder.expand_word(j, one), [foo]) self.assertEqual(Placeholder.expand_word(j, two), [foo]) j = '{' + p[0:1] + '}' self.assertEqual(Placeholder.expand_word(j, zero), []) self.assertEqual(Placeholder.expand_word(j, one), [foo]) self.assertEqual(Placeholder.expand_word(j, two), [foo]) j = jbos('foo') self.assertEqual(Placeholder.expand_word(j, zero), [j]) self.assertEqual(Placeholder.expand_word(j, one), [j]) self.assertEqual(Placeholder.expand_word(j, two), [j]) with self.assertRaises(ValueError): Placeholder.expand_word(p + p, zero)
def test_concat_path(self): self.assertEqual( Variable('foo') + path.Path('bar'), safe_str.jbos(safe_str.literal('$(foo)'), path.Path('bar'))) self.assertEqual( path.Path('foo') + Variable('bar'), safe_str.jbos(path.Path('foo'), safe_str.literal('$(bar)')))
def test_concat_path(self): self.assertEqual( Pattern('%.c') + path.Path('bar'), safe_str.jbos(safe_str.literal('%'), '.c', path.Path('bar'))) self.assertEqual( path.Path('foo') + Pattern('%.h'), safe_str.jbos(path.Path('foo'), safe_str.literal('%'), '.h'))
def test_concat_str(self): self.assertEqual(Variable('foo') + 'bar', safe_str.jbos( safe_str.literal('${foo}'), 'bar' )) self.assertEqual('foo' + Variable('bar'), safe_str.jbos( 'foo', safe_str.literal('${bar}') ))
def test_concat_str(self): self.assertEqual(Pattern('%.c') + 'bar', safe_str.jbos( safe_str.literal('%'), '.cbar' )) self.assertEqual('foo' + Pattern('%.h'), safe_str.jbos( 'foo', safe_str.literal('%'), '.h' ))
def test_concat_var(self): self.assertEqual( Variable('foo') + Variable('bar'), safe_str.jbos(safe_str.literal('$(foo)'), safe_str.literal('$(bar)'))) self.assertEqual( Variable('foo', True) + Variable('bar'), safe_str.jbos(safe_str.literal("'$(foo)'"), safe_str.literal('$(bar)')))
def test_concat_var(self): self.assertEqual( Function('foo', '1', '2') + Function('bar', '3', '4'), safe_str.jbos(safe_str.literal('$(foo 1,2)'), safe_str.literal('$(bar 3,4)'))) self.assertEqual( Function('foo', '1', '2', quoted=True) + Function('bar', '3', '4'), safe_str.jbos(safe_str.literal("'$(foo 1,2)'"), safe_str.literal('$(bar 3,4)')))
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_concat_str(self): self.assertEqual( Function('fn', '1', '2') + 'foo', safe_str.jbos(safe_str.literal('$(fn 1,2)'), 'foo')) self.assertEqual('foo' + Function('fn', '1', '2'), safe_str.jbos('foo', safe_str.literal('$(fn 1,2)'))) self.assertEqual( Function('fn', '1', '2', quoted=True) + 'foo', safe_str.jbos(safe_str.literal("'$(fn 1,2)'"), 'foo')) self.assertEqual('foo' + Function('fn', '1', '2', quoted=True), safe_str.jbos('foo', safe_str.literal("'$(fn 1,2)'")))
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_concat_path(self): self.assertEqual( Function('fn', '1', '2') + path.Path('foo'), safe_str.jbos(safe_str.literal('$(fn 1,2)'), path.Path('foo'))) self.assertEqual( path.Path('foo') + Function('fn', '1', '2'), safe_str.jbos(path.Path('foo'), safe_str.literal('$(fn 1,2)'))) self.assertEqual( Function('fn', '1', '2', quoted=True) + path.Path('foo'), safe_str.jbos(safe_str.literal("'$(fn 1,2)'"), path.Path('foo'))) self.assertEqual( path.Path('foo') + Function('fn', '1', '2', quoted=True), safe_str.jbos(path.Path('foo'), safe_str.literal("'$(fn 1,2)'")))
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_shell(self): s = safe_str.jbos('$foo', safe_str.literal('$bar')) self.out.write(s, Syntax.shell) if platform_info().family == 'windows': expected = '$$foo$bar' else: expected = quoted('$$foo') + '$bar' self.assertEqual(self.out.stream.getvalue(), expected)
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_write_jbos_shell(self): out = Writer(StringIO()) s = safe_str.jbos('$foo', safe_str.escaped_str('$bar')) out.write(s, Syntax.shell) if platform_name() == 'windows': expected = '$$foo$bar' else: expected = quoted('$$foo') + '$bar' self.assertEqual(out.stream.getvalue(), expected)
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_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_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_flags_lib(self): lib1 = Path('/path/to/lib/libfoo.jar') lib2 = Path('/path/to/lib/libbar.jar') self.assertEqual( self.compiler.flags( opts.option_list( opts.lib(file_types.StaticLibrary(lib1, 'jvm')))), ['-cp', jbos(lib1)]) self.assertEqual( self.compiler.flags( opts.option_list( opts.lib(file_types.StaticLibrary(lib1, 'jvm')), opts.lib(file_types.StaticLibrary(lib2, 'jvm')))), ['-cp', lib1 + os.pathsep + lib2])
def test_jbos(self): self.assertEqual( windows.escape_line(jbos('foo', literal('bar'))), shell_list([jbos(shell_literal('foo'), literal('bar'))]))
def test_write_jbos_function(self): out = Writer(StringIO()) s = safe_str.jbos('$foo', safe_str.escaped_str('$bar')) out.write(s, Syntax.function) self.assertEqual(out.stream.getvalue(), quoted('$$foo') + '$bar')
def test_flags_lib_dir(self): libdir = Path('/path/to/lib') lib = Path('/path/to/lib/libfoo.a') output = file_types.Executable(Path('exe'), 'native') if self.env.target_platform.name == 'linux': rpath = rpath_with_output = ['-Wl,-rpath,' + libdir] elif self.env.target_platform.name == 'darwin': rpath = [] rpath_with_output = [jbos('-Wl,-rpath,', '@loader_path')] else: rpath = rpath_with_output = [] # Lib dir self.assertEqual( self.linker.flags( opts.option_list(opts.lib_dir(file_types.Directory(libdir)))), ['-L' + libdir]) # Shared library self.assertEqual( self.linker.flags( opts.option_list( opts.lib(file_types.SharedLibrary(lib, 'native')))), ['-L' + libdir] + rpath) self.assertEqual( self.linker.flags( opts.option_list( opts.lib(file_types.SharedLibrary(lib, 'native'))), output), ['-L' + libdir] + rpath_with_output) if self.env.target_platform.name == 'linux': libdir2 = Path('foo') lib2 = Path('foo/libbar.a') with self.assertRaises(ValueError): self.linker.flags( opts.option_list( opts.lib(file_types.SharedLibrary(lib2, 'native')))) self.assertEqual( self.linker.flags( opts.option_list( opts.lib(file_types.SharedLibrary(lib2, 'native'))), output), ['-L' + libdir2, jbos('-Wl,-rpath,', '$ORIGIN/foo')]) # Static library self.assertEqual( self.linker.flags( opts.option_list( opts.lib(file_types.StaticLibrary(lib, 'native')))), []) self.assertEqual( self.linker.flags(opts.option_list( opts.lib(file_types.StaticLibrary(lib, 'native'))), mode='pkg-config'), ['-L' + libdir]) # Generic library self.assertEqual( self.linker.flags( opts.option_list(opts.lib(file_types.Library(lib, 'native')))), ['-L' + libdir]) mingw_lib = Path('/path/to/lib/foo.lib') self.assertEqual( self.linker.flags( opts.option_list( opts.lib(file_types.Library(mingw_lib, 'native')))), []) with self.assertRaises(ValueError): self.linker.flags(opts.option_list( opts.lib(file_types.Library(mingw_lib, 'native'))), mode='pkg-config') # Framework self.assertEqual( self.linker.flags(opts.option_list(opts.lib(Framework('cocoa')))), []) # Mixed self.assertEqual( self.linker.flags( opts.option_list( opts.lib_dir(file_types.Directory(libdir)), opts.lib(file_types.SharedLibrary(lib, 'native')))), ['-L' + libdir] + rpath) self.assertEqual( self.linker.flags( opts.option_list( opts.lib_dir(file_types.Directory(libdir)), opts.lib(file_types.SharedLibrary(lib, 'native'))), output), ['-L' + libdir] + rpath_with_output)
def test_jbos(self): j = jbos('foo', literal('='), 'bar baz') self.assertEqual(textify(j), 'foo=bar baz') self.assertEqual(textify(j, True), 'foo="bar baz"')
def test_concat_pattern(self): self.assertEqual( Pattern('%.c') + Pattern('%.h'), safe_str.jbos(safe_str.literal('%'), '.c', safe_str.literal('%'), '.h'))
def test_clean(self): out = Writer(StringIO()) s = safe_str.jbos('$foo', safe_str.literal('$bar')) out.write(s, Syntax.clean) self.assertEqual(out.stream.getvalue(), '$$foo$bar')
def test_shell(self): out = Writer(StringIO()) s = safe_str.jbos('$foo', safe_str.literal('$bar')) out.write(s, Syntax.shell) self.assertEqual(out.stream.getvalue(), quoted('$$foo') + '$bar')
def test_jbos(self): self.assertEqual(convert_args([jbos('foo', 'bar')]), ['foobar']) self.assertEqual(convert_args([jbos('foo', self.Path('/bar'))]), ['foo' + self.ospath.sep + 'bar'])
def test_write_jbos_input(self): out = Writer(StringIO()) s = safe_str.jbos('$foo', safe_str.escaped_str('$bar')) out.write(s, Syntax.input) self.assertEqual(out.stream.getvalue(), '$$foo$bar')
def test_input(self): s = safe_str.jbos('$foo', safe_str.literal('$bar')) self.out.write(s, Syntax.input) self.assertEqual(self.out.stream.getvalue(), '$$foo$bar')