def known_commits(self): with temporary_dir(root_dir=get_buildroot()) as worktree: with safe_open(os.path.join(worktree, 'README'), 'w') as fp: fp.write('Just a test tree.') with initialize_repo(worktree=worktree, gitdir=os.path.join(worktree, '.git')) as git: src_file = os.path.join(worktree, 'src/java/org/pantsbuild/Class.java') with safe_open(src_file, 'w') as fp: fp.write(dedent(""" package org.pantsbuild; class Class { static final int MEANING_OF_LIFE = 42; } """)) src_build_file = os.path.join(worktree, 'src/java/org/pantsbuild/BUILD') with safe_open(src_build_file, 'w') as fp: fp.write("java_library(name='pantsbuild', sources=['Class.java'])") git.add(src_file, src_build_file) git.commit('Introduce Class.') test_file = os.path.join(worktree, 'tests/java/org/pantsbuild/ClassTest.java') with safe_open(test_file, 'w') as fp: fp.write(dedent(""" package org.pantsbuild; import org.junit.Assert; import org.junit.Test; public class ClassTest { @Test public void test() { Assert.assertEquals(42, Class.MEANING_OF_LIFE); } } """)) test_build_file = os.path.join(worktree, 'tests/java/org/pantsbuild/BUILD') with safe_open(test_build_file, 'w') as fp: fp.write(dedent(f""" jar_library(name='junit', jars=[jar('junit', 'junit', '4.12')]) junit_tests( name='pantsbuild', sources=['ClassTest.java'], dependencies=[ ':junit', '{os.path.relpath(os.path.dirname(src_build_file), get_buildroot())}' ] ) """)) git.add(test_file, test_build_file) git.commit('Introduce ClassTest.') yield
def _create_tiny_git_repo(self, *, copy_files: Optional[Sequence[Path]] = None): with temporary_dir() as gitdir, temporary_dir() as worktree: # A tiny little fake git repo we will set up. initialize_repo() requires at least one file. Path(worktree, "README").touch() # The contextmanager interface is only necessary if an explicit gitdir is not provided. with initialize_repo(worktree, gitdir=gitdir) as git: if copy_files is not None: for fp in copy_files: new_fp = Path(worktree, fp) safe_mkdir_for(str(new_fp)) shutil.copy(fp, new_fp) yield git, worktree, gitdir
def create_isolated_git_repo(): # Isolated Git Repo Structure: # worktree # |--README # |--pants.toml # |--src # |--resources # |--org/pantsbuild/resourceonly # |--BUILD # |--README.md # |--python # |--python_targets # |--BUILD # |--test_binary.py # |--test_library.py # |--test_unclaimed_src.py # |--sources # |--BUILD # |--sources.py # |--sources.txt with temporary_dir(root_dir=get_buildroot()) as worktree: def create_file(path, content): """Creates a file in the isolated git repo.""" return create_file_in(worktree, path, content) def copy_into(path, to_path=None): """Copies a file from the real git repo into the isolated git repo.""" write_path = os.path.join(worktree, to_path or path) if os.path.isfile(path): safe_mkdir(os.path.dirname(write_path)) shutil.copyfile(path, write_path) else: shutil.copytree(path, write_path) return write_path create_file("README", "N.B. This is just a test tree.") create_file( "pants.toml", f""" [GLOBAL] pythonpath = ["{get_buildroot()}/pants-plugins/src/python"] backend_packages2.add = ["pants.backend.python", "internal_backend.utilities"] """, ) copy_into(".gitignore") with initialize_repo(worktree=worktree, gitdir=os.path.join(worktree, ".git")) as git: def add_to_git(commit_msg, *files): git.add(*files) git.commit(commit_msg) add_to_git( "resource file", create_file( "src/resources/org/pantsbuild/resourceonly/BUILD", """ resources( name='resource', sources=['README.md'] ) """, ), create_file( "src/resources/org/pantsbuild/resourceonly/README.md", "Just a resource." ), ) add_to_git( "python targets", copy_into("testprojects/src/python/python_targets", "src/python/python_targets"), ) add_to_git( 'a python_library with resources=["filename"]', copy_into("testprojects/src/python/sources", "src/python/sources"), ) with environment_as(PANTS_BUILDROOT_OVERRIDE=worktree): yield worktree
def create_isolated_git_repo(): # Isolated Git Repo Structure: # worktree # |--README # |--pants.ini # |--3rdparty # |--BUILD # |--src # |--resources # |--org/pantsbuild/resourceonly # |--BUILD # |--README.md # |--java # |--org/pantsbuild/helloworld # |--BUILD # |--helloworld.java # |--python # |--python_targets # |--BUILD # |--test_binary.py # |--test_library.py # |--test_unclaimed_src.py # |--sources # |--BUILD # |--sources.py # |--sources.txt # |--tests # |--scala # |--org/pantsbuild/cp-directories # |--BUILD # |--ClasspathDirectoriesSpec.scala with temporary_dir(root_dir=get_buildroot()) as worktree: def create_file(path, content): """Creates a file in the isolated git repo.""" return create_file_in(worktree, path, content) def copy_into(path, to_path=None): """Copies a file from the real git repo into the isolated git repo.""" write_path = os.path.join(worktree, to_path or path) if os.path.isfile(path): safe_mkdir(os.path.dirname(write_path)) shutil.copyfile(path, write_path) else: shutil.copytree(path, write_path) return write_path create_file('README', 'N.B. This is just a test tree.') create_file('pants.ini', """ [GLOBAL] pythonpath: [ "{0}/contrib/go/src/python", "{0}/pants-plugins/src/python" ] backend_packages: +[ "internal_backend.utilities", "pants.contrib.go" ] """.format(get_buildroot()) ) copy_into('.gitignore') with initialize_repo(worktree=worktree, gitdir=os.path.join(worktree, '.git')) as git: def add_to_git(commit_msg, *files): git.add(*files) git.commit(commit_msg) add_to_git('a go target with default sources', create_file('src/go/tester/BUILD', 'go_binary()'), create_file('src/go/tester/main.go', """ package main import "fmt" func main() { fmt.Println("hello, world") } """ ) ) add_to_git('resource file', create_file('src/resources/org/pantsbuild/resourceonly/BUILD', """ resources( name='resource', sources=['README.md'] ) """ ), create_file('src/resources/org/pantsbuild/resourceonly/README.md', 'Just a resource.') ) add_to_git('hello world java program with a dependency on a resource file', create_file('src/java/org/pantsbuild/helloworld/BUILD', """ jvm_binary( dependencies=[ 'src/resources/org/pantsbuild/resourceonly:resource', ], source='helloworld.java', main='org.pantsbuild.helloworld.HelloWorld', ) """ ), create_file('src/java/org/pantsbuild/helloworld/helloworld.java', """ package org.pantsbuild.helloworld; class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!\n"); } } """ ) ) add_to_git('scala test target', copy_into( 'testprojects/tests/scala/org/pantsbuild/testproject/cp-directories', 'tests/scala/org/pantsbuild/cp-directories' ) ) add_to_git('python targets', copy_into('testprojects/src/python/python_targets', 'src/python/python_targets') ) add_to_git('a python_library with resources=["filename"]', copy_into('testprojects/src/python/sources', 'src/python/sources') ) add_to_git('3rdparty/BUILD', copy_into('3rdparty/BUILD')) with environment_as(PANTS_BUILDROOT_OVERRIDE=worktree): yield worktree