def test_relpath_relative(self): p = self.Path('foo/bar', path.Root.srcdir) self.assertEqual(p.relpath(p), '.') self.assertEqual(p.relpath(p, 'pre'), 'pre') self.assertEqual(p.relpath(self.Path('foo', path.Root.srcdir)), 'bar') self.assertEqual(p.relpath(self.Path('foo', path.Root.srcdir), 'pre'), self.ospath.join('pre', 'bar')) self.assertEqual(p.relpath(self.Path('baz', path.Root.srcdir)), self.ospath.join('..', 'foo', 'bar')) self.assertEqual(p.relpath(self.Path('baz', path.Root.srcdir), 'pre'), self.ospath.join('pre', '..', 'foo', 'bar')) self.assertEqual(p.relpath(self.Path('.', path.Root.srcdir)), self.ospath.join('foo', 'bar')) self.assertEqual(p.relpath(self.Path('.', path.Root.srcdir), 'pre'), self.ospath.join('pre', 'foo', 'bar')) p = self.Path('.', path.Root.srcdir) self.assertEqual(p.relpath(path.Path('foo', path.Root.srcdir)), '..') self.assertEqual(p.relpath(path.Path('foo', path.Root.srcdir), 'pre'), self.ospath.join('pre', '..')) p = self.Path('foo/bar', path.Root.srcdir) self.assertRaises( ValueError, lambda: p.relpath(self.Path('foo', path.Root.builddir)))
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_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_drive(self): with mock.patch('os.getcwd', return_value=r'C:\base'): self.assertEqual(path.abspath('foo'), path.Path('C:/base/foo', path.Root.absolute)) self.assertEqual(path.abspath('/foo/bar'), path.Path('C:/foo/bar', path.Root.absolute)) self.assertEqual(path.abspath('D:/foo/bar'), path.Path('D:/foo/bar', path.Root.absolute))
def test_polyfill(self): class OsPath(object): def __init__(self): for i in ('isabs', 'normpath', 'realpath', 'expanduser'): setattr(self, i, getattr(os.path, i)) with mock.patch('os.path', OsPath()): self.assertEqual( path.samefile(path.Path('/foo/bar'), path.Path('/foo/bar')), True)
def test_install_path_directory(self): p = path.Path('foo/bar', path.Root.srcdir) self.assertEqual(path.install_path(p, path.InstallRoot.bindir, True), path.Path('', path.InstallRoot.bindir, True)) p = path.Path('foo/bar', path.Root.builddir) self.assertEqual(path.install_path(p, path.InstallRoot.bindir, True), path.Path('foo/bar', path.InstallRoot.bindir, True)) p = path.abspath('/foo/bar') with self.assertRaises(TypeError): path.install_path(p, path.InstallRoot.bindir, True)
def test_abspath(self): with mock.patch('os.getcwd', return_value=r'/base'): self.assertPathEqual(path.abspath('foo'), path.Path('/base/foo', path.Root.absolute)) self.assertPathEqual(path.abspath('/foo/bar'), path.Path('/foo/bar', path.Root.absolute)) self.assertPathEqual(path.abspath('foo/'), path.Path('/base/foo/', path.Root.absolute)) self.assertPathEqual(path.abspath('/foo/bar/'), path.Path('/foo/bar/', path.Root.absolute)) self.assertPathEqual(path.abspath('foo', directory=True), path.Path('/base/foo/', path.Root.absolute)) self.assertRaises(ValueError, path.abspath, 'foo/', directory=False)
def test_not_found(self): def mock_listdir(path): raise OSError() with mock.patch('os.listdir', mock_listdir): dirs, nondirs = path.listdir(path.Path('.'), self.path_vars) self.assertEqual(dirs, []) self.assertEqual(nondirs, [])
def test_destdir(self): out = self.ninjafile.writer(StringIO()) self.ninjafile._write_variable( out, Variable('name'), path.Path('foo', path.InstallRoot.bindir, destdir=True)) self.assertEqual( out.stream.getvalue(), 'name = {}\n'.format(quoted(os.path.join('${bindir}', 'foo')))) ninjafile = NinjaFile('build.bfg', destdir=True) out = ninjafile.writer(StringIO()) ninjafile._write_variable( out, Variable('name'), path.Path('foo', path.InstallRoot.bindir, destdir=True)) self.assertEqual( out.stream.getvalue(), 'name = {}\n'.format( quoted(os.path.join('${DESTDIR}${bindir}', 'foo'))))
def test_destdir(self): out = self.makefile.writer(StringIO()) self.makefile._write_variable( out, Variable('name'), path.Path('foo', path.InstallRoot.bindir, destdir=True)) self.assertEqual( out.stream.getvalue(), 'name := {}\n'.format(quoted(os.path.join('$(bindir)', 'foo')))) makefile = Makefile('build.bfg', destdir=True) out = makefile.writer(StringIO()) makefile._write_variable( out, Variable('name'), path.Path('foo', path.InstallRoot.bindir, destdir=True)) self.assertEqual( out.stream.getvalue(), 'name := {}\n'.format( quoted(os.path.join('$(DESTDIR)$(bindir)', 'foo'))))
def test_install_path_cross_directory(self): for name in ('winnt', 'linux'): platform = target.platform_info(name) env = MockEnv(platform) p = path.Path('foo/bar', path.Root.srcdir) self.assertEqual( path.install_path(p, path.InstallRoot.bindir, True, cross=env), platform.Path('', path.InstallRoot.bindir)) p = path.Path('foo/bar', path.Root.builddir) self.assertEqual( path.install_path(p, path.InstallRoot.bindir, True, cross=env), platform.Path('foo/bar', path.InstallRoot.bindir)) p = path.Path('/foo/bar', path.Root.absolute) self.assertEqual( path.install_path(p, path.InstallRoot.bindir, True, cross=env), platform.Path('/foo/bar', path.Root.absolute))
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_rule(self): self.makefile.rule('target', variables={'name': 'value'}, recipe=['cmd']) out = Writer(StringIO()) self.makefile._write_rule(out, self.makefile._rules[0]) self.assertEqual(out.stream.getvalue(), 'target: name := value\n' 'target:\n' '\tcmd\n\n') # Test duplicate targets. self.assertRaises(ValueError, self.makefile.rule, 'target') self.assertRaises(ValueError, self.makefile.rule, ['target', 'target2']) self.assertRaises(ValueError, self.makefile.rule, File(path.Path('target', path.Root.builddir)))
def test_build(self): self.ninjafile.rule('my_rule', ['cmd']) self.ninjafile.build('output', 'my_rule') out = Writer(StringIO()) self.ninjafile._write_build(out, self.ninjafile._builds[0]) self.assertEqual(out.stream.getvalue(), 'build output: my_rule\n') # Test duplicate targets. self.assertRaises(ValueError, self.ninjafile.build, 'output', 'my_rule') self.assertRaises(ValueError, self.ninjafile.build, ['output', 'output2'], 'my_rule') self.assertRaises(ValueError, self.ninjafile.build, File(path.Path('output', path.Root.builddir)), 'my_rule')
def test_concat_path(self): jbos, lit = safe_str.jbos, safe_str.literal fn = syntax_string(jbos(lit('$(fn '), '1', lit(','), '2', lit(')')), Syntax.function) self.assertEqual( Function('fn', '1', '2') + path.Path('foo'), jbos(fn, path.Path('foo'))) self.assertEqual( path.Path('foo') + Function('fn', '1', '2'), jbos(path.Path('foo'), fn)) fn = syntax_string(jbos(lit('$(fn '), '1', lit(','), '2', lit(')')), Syntax.function, True) self.assertEqual( (Function('fn', '1', '2', quoted=True) + path.Path('foo')), jbos(fn, path.Path('foo'))) self.assertEqual( (path.Path('foo') + Function('fn', '1', '2', quoted=True)), jbos(path.Path('foo'), fn))
def test_rule(self): self.makefile.rule('target', variables={'name': 'value'}, recipe=['cmd']) out = self.makefile.writer(StringIO()) self.makefile._write_rule(out, self.makefile._rules[-1]) self.assertEqual(out.stream.getvalue(), 'target: name := value\n' 'target:\n' '\tcmd\n\n') self.makefile.rule('silent-target', recipe=[Silent('cmd')], phony=True) out = self.makefile.writer(StringIO()) self.makefile._write_rule(out, self.makefile._rules[-1]) self.assertEqual( out.stream.getvalue(), '.PHONY: silent-target\n' 'silent-target:\n' '\t@cmd\n\n') self.makefile.rule('call-target', recipe=Call('fn', '1', '2')) out = self.makefile.writer(StringIO()) self.makefile._write_rule(out, self.makefile._rules[-1]) self.assertEqual(out.stream.getvalue(), 'call-target: ; $(call fn,1,2)\n\n') self.makefile.rule('empty-target') out = self.makefile.writer(StringIO()) self.makefile._write_rule(out, self.makefile._rules[-1]) self.assertEqual(out.stream.getvalue(), 'empty-target:\n\n') # Test duplicate targets. self.assertRaises(ValueError, self.makefile.rule, 'target') self.assertRaises(ValueError, self.makefile.rule, ['target', 'target2']) self.assertRaises(ValueError, self.makefile.rule, File(path.Path('target', path.Root.builddir))) # Test no targets. self.assertRaises(ValueError, self.makefile.rule, [])
def test_build(self): self.ninjafile.rule('my_rule', ['cmd']) self.ninjafile.build('output', 'my_rule') out = self.ninjafile.writer(StringIO()) self.ninjafile._write_build(out, self.ninjafile._builds[-1]) self.assertEqual(out.stream.getvalue(), 'build output: my_rule\n') self.ninjafile.build('doutput', 'my_rule', inputs='input', implicit='implicit', order_only='order') out = self.ninjafile.writer(StringIO()) self.ninjafile._write_build(out, self.ninjafile._builds[-1]) self.assertEqual(out.stream.getvalue(), 'build doutput: my_rule input | implicit || order\n') self.ninjafile.build('voutput', 'my_rule', variables={'var': 'value'}) out = self.ninjafile.writer(StringIO()) self.ninjafile._write_build(out, self.ninjafile._builds[-1]) self.assertEqual(out.stream.getvalue(), 'build voutput: my_rule\n' ' var = value\n') # Test duplicate targets. self.assertRaises(ValueError, self.ninjafile.build, 'output', 'my_rule') self.assertRaises(ValueError, self.ninjafile.build, ['output', 'output2'], 'my_rule') self.assertRaises(ValueError, self.ninjafile.build, File(path.Path('output', path.Root.builddir)), 'my_rule') # Test unknown rule. self.assertRaises(ValueError, self.ninjafile.build, 'output2', 'unknown_rule')
def test_write_path_shell(self): out = Writer(StringIO()) out.write(path.Path('foo', path.Root.srcdir), Syntax.shell) self.assertEqual(out.stream.getvalue(), quoted(os.path.join('$srcdir', 'foo')))
def test_write_path_input(self): out = Writer(StringIO()) out.write(path.Path('foo', path.Root.srcdir), Syntax.input) self.assertEqual(out.stream.getvalue(), os.path.join('$srcdir', 'foo'))
def test_single(self): p = path.Path('foo/bar') self.assertEqual(path.commonprefix([p]), p)
def test_different_roots(self): sab = path.Path('a/b', path.Root.srcdir) sabc = path.Path('a/b/c', path.Root.srcdir) bab = path.Path('a/b', path.Root.builddir) babc = path.Path('a/b/c', path.Root.builddir) self.assertEqual(path.uniquetrees([sabc, bab, babc, sab]), [sab, bab])
def test_real(self): with mock.patch('os.path.samefile', lambda x, y: x == y, create=True): self.assertEqual( path.samefile(path.Path('/foo/bar'), path.Path('/foo/bar')), True)
def test_wrap(self): mocked = mock.MagicMock(return_value=True) mocked.__name__ = 'foo' f = path._wrap_ospath(mocked) f(path.Path('/foo/bar')) mocked.assert_called_once_with(os.path.join(os.path.sep, 'foo', 'bar'))
def test_multi_no_match(self): p = path.Path('foo/bar') q = path.Path('baz/quux') self.assertEqual(path.commonprefix([p, q]), path.Path(''))
def test_multi_subset(self): p = path.Path('foo/bar') q = path.Path('foo/bar/baz') self.assertEqual(path.commonprefix([p, q]), p)
def test_listdir(self): with mock_filesystem(): dirs, nondirs = path.listdir(path.Path('.'), self.path_vars) self.assertPathListEqual(dirs, [path.Path('dir/')]) self.assertPathListEqual(nondirs, [path.Path('file.cpp')])
def test_multi_same(self): p = path.Path('foo/bar') self.assertEqual(path.commonprefix([p, p]), p)
def test_write_path_dependency(self): out = Writer(StringIO()) out.write(path.Path('foo', path.Root.srcdir), Syntax.dependency) self.assertEqual(out.stream.getvalue(), os.path.join('$(srcdir)', 'foo'))
def test_multi_partial_match(self): p = path.Path('foo/bar') q = path.Path('foo/baz') self.assertEqual(path.commonprefix([p, q]), p.parent())
def test_not_exists(self): with mock.patch('bfg9000.path.exists', return_value=False): self.assertEqual(list(path.walk(path.Path('.'), self.path_vars)), [])