def test_fails_on_parent(self): os.mkdir('u') with testenv.DirectoryChanger('u'): os.mkdir('.dlbroot') with dlb.ex.Context(): with self.assertRaises(ValueError): dlb.ex.Context.active.working_tree_path_of('../')
def test_detects_included_files_with_unrepresentable_character_in_abspath( self): strange_dir_name = '统一码' # not in cp437 or cp850 os.mkdir(strange_dir_name) with testenv.DirectoryChanger(strange_dir_name): os.mkdir('.dlbroot') open('a.h', 'xb').close() with open('a.c', 'xb') as f: f.write(b'#include "a.h"\n') dlb.di.set_threshold_level(dlb.di.INFO) dlb.di.set_output_file(sys.stderr) binary_path = dlb.fs.Path(dlb.fs.Path.Native(vctools_install_dir), is_dir=True) / 'bin/Hostx64/x64/' t = CCompiler(source_files=['a.c'], object_files=['a.o']) with dlb.ex.Context(): # see <program-dir>\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars*.bat dlb.ex.Context.active.env.import_from_outer( 'SYSTEMROOT', pattern=r'.+', example='C:\\WINDOWS') dlb.ex.Context.active.env.import_from_outer( 'INCLUDE', pattern=r'[^;]+(;[^;]+)*', example='C:\\X;D:\\Y') dlb.ex.Context.active.env['INCLUDE'] = os.getcwd() dlb.ex.Context.active.helper['cl.exe'] = binary_path / 'cl.exe' result = t.start() self.assertEqual((dlb.fs.Path('a.h'), ), result.included_files)
def test_entering_fails_if_path_not_representable(self): os.mkdir('x y') with testenv.DirectoryChanger('x y'): os.mkdir('.dlbroot') regex = ( r"(?m)\A" r"current directory violates imposed path restrictions\n" r" \| reason: .*NoSpacePath.*'.+'.*\n" r" \| move the working directory or choose a less restrictive path class for the root context\Z" ) with self.assertRaisesRegex(ValueError, regex): with dlb.ex.Context(path_cls=dlb.fs.NoSpacePath): pass with dlb.ex.Context(): # no exception regex = ( r"(?m)\A" r"working tree's root path violates path restrictions imposed by this context\n" r" \| reason: .*NoSpacePath.*'.+'.*\n" r" \| move the working directory or choose a less restrictive path class for the root context\Z" ) with self.assertRaisesRegex(ValueError, regex): with dlb.ex.Context(path_cls=dlb.fs.NoSpacePath): pass
def test_makes_relative_to_working_tree_root(self): os.mkdir('x') os.makedirs('d/a/b') with dlb.ex.Context(): with testenv.DirectoryChanger('x'): p = dlb.ex.Context().find_path_in('a/b/', ['d/']) self.assertEqual(dlb.ex.Context.active.root_path / 'd/a/b/', p)
def test_absolute_can_be_outside_managed_tree(self): open('x.cpp', 'xb').close() os.mkdir('t') with testenv.DirectoryChanger('t'): os.mkdir('.dlbroot') open('a.cpp', 'xb').close() with dlb.ex.Context() as c: t = ATool(source_file=c.root_path / '../x.cpp', object_file='a.o') t.start()
def test_fails_for_lf_in_cwd(self): os.mkdir('out\nput') with testenv.DirectoryChanger('out\nput'): os.mkdir('.dlbroot') open('report.tex', 'xb').close() with self.assertRaises(Exception) as cm: with dlb.ex.Context(): dlb_contrib.tex.Tex(toplevel_file='report.tex', output_file='report.dvi', state_files=[]).start() self.assertEqual( "current working directory must not contain '\\n'", str(cm.exception))
def test_fails_for_absolute_path_outside_working_tree(self): os.makedirs(os.path.join('a', 'b', 'c')) os.makedirs(os.path.join('a', 'b2', 'c2')) old_cw = os.getcwd() with testenv.DirectoryChanger(os.path.join('a', 'b')): os.mkdir('.dlbroot') with dlb.ex.Context(): with self.assertRaises(dlb.ex.WorkingTreePathError) as cm: dlb.ex.Context.active.working_tree_path_of( dlb.fs.Path.Native(os.path.join(old_cw, 'a', 'b2', 'c2'))) msg = "does not start with the working tree's root path" self.assertEqual(msg, str(cm.exception))
def test_example(self): origin_repo_dir = os.path.abspath(tempfile.mkdtemp()) with testenv.DirectoryChanger(origin_repo_dir): subprocess.check_output(['git', '-c', 'init.DefaultBranch=master', 'init']) subprocess.check_output(['git', 'config', 'user.email', '*****@*****.**']) subprocess.check_output(['git', 'config', 'user.name', 'user.name']) subprocess.check_output(['touch', 'x']) subprocess.check_output(['git', 'add', 'x']) subprocess.check_output(['git', 'commit', '-m', 'Initial commit']) subprocess.check_output(['git', 'tag', '-a', 'v1.2.3', '-m', 'Release']) subprocess.check_output(['git', '-c', 'init.DefaultBranch=master', 'init']) subprocess.check_output(['git', 'remote', 'add', 'origin', origin_repo_dir]) subprocess.check_output(['git', 'fetch']) subprocess.check_output(['git', 'fetch', '--tags']) with dlb.ex.Context(): class GitCheckTags(dlb_contrib.git.GitCheckTags): ANNOTATED_TAG_NAME_REGEX = r'v(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*)){2}' # e.g. 'v1.23.0' version_tag_names = set(GitCheckTags().start().commit_by_annotated_tag_name) self.assertEqual({'v1.2.3'}, version_tag_names)
def test_remote_too(self): class GitCheckTags(dlb_contrib.git.GitCheckTags): pass class GitCheckTags2(GitCheckTags): DO_SYNC_CHECK_LIGHTWEIGHT_TAGS = True origin_repo_dir = os.path.abspath(tempfile.mkdtemp()) with testenv.DirectoryChanger(origin_repo_dir): subprocess.check_output(['git', '-c', 'init.DefaultBranch=master', 'init']) subprocess.check_output(['git', 'config', 'user.email', '*****@*****.**']) subprocess.check_output(['git', 'config', 'user.name', 'user.name']) subprocess.check_output(['touch', 'x']) subprocess.check_output(['git', 'add', 'x']) subprocess.check_output(['git', 'commit', '-m', 'Initial commit']) subprocess.check_output(['git', 'tag', '-a', 'v1.2.3c4', '-m', 'Release']) subprocess.check_output(['touch', 'y']) subprocess.check_output(['git', 'add', 'y']) subprocess.check_output(['git', 'commit', '-m', 'Add y']) subprocess.check_output(['git', 'tag', '-a', 'v2.0.0', '-m', 'Release']) subprocess.check_output(['git', 'tag', '-a', 'v2.0.1', '-m', 'Release']) subprocess.check_output(['git', 'tag', 'vm']) subprocess.check_output(['git', 'tag', 'v']) subprocess.check_output(['git', 'tag', 'w']) subprocess.check_output(['git', '-c', 'init.DefaultBranch=master', 'init']) subprocess.check_output(['touch', 'x']) subprocess.check_output(['git', 'add', 'x']) subprocess.check_output(['git', 'commit', '-m', 'Initial commit']) subprocess.check_output(['git', 'remote', 'add', 'origin', origin_repo_dir]) subprocess.check_output(['git', 'fetch']) subprocess.check_output(['git', 'fetch', '--tags']) with dlb.ex.Context(): GitCheckTags().start() with dlb.ex.Context(): subprocess.check_output(['git', 'tag', '-d', 'vm']) subprocess.check_output(['git', 'tag', '-d', 'v']) GitCheckTags().start() # do not sync lightweight tags by default with self.assertRaises(ValueError) as cm: GitCheckTags2().start().complete() msg = "remote tags missing locally: 'v', 'vm'" self.assertEqual(msg, str(cm.exception)) subprocess.check_output(['git', 'tag', '-d', 'v1.2.3c4']) subprocess.check_output(['git', 'tag', '-d', 'v2.0.1']) with self.assertRaises(ValueError) as cm: GitCheckTags().start().complete() msg = "remote tags missing locally: 'v1.2.3c4', 'v2.0.1'" self.assertEqual(msg, str(cm.exception)) subprocess.check_output(['git', 'tag', '-a', 'v1.2.3c4', '-m', 'Release']) # different commit subprocess.check_output(['git', 'tag', '-a', 'v2.0.1', '-m', 'Release']) # different commit with self.assertRaises(ValueError) as cm: GitCheckTags().start().complete() msg = "tags for different commits locally and remotely: 'v1.2.3c4', 'v2.0.1'" self.assertEqual(msg, str(cm.exception)) subprocess.check_output(['git', 'tag', '-a', 'v3.0.0', '-m', 'Release']) subprocess.check_output(['git', 'tag', '-a', 'v3.0.1', '-m', 'Release']) with self.assertRaises(ValueError) as cm: GitCheckTags().start().complete() msg = "local tags missing on remotely: 'v3.0.0', 'v3.0.1'" self.assertEqual(msg, str(cm.exception))