Example #1
0
 def _create_tiny_git_repo(self):
   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.
     readme_file = os.path.join(worktree, 'README')
     touch(readme_file)
     # The contextmanager interface is only necessary if an explicit gitdir is not provided.
     with initialize_repo(worktree, gitdir=gitdir) as git:
       yield git, worktree, gitdir
  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("""
          jar_library(name='junit', jars=[jar('junit', 'junit', '4.12')])

          junit_tests(
            name='pantsbuild',
            sources=['ClassTest.java'],
            dependencies=[
              ':junit',
              '{}'
            ]
          )
          """).format(os.path.relpath(os.path.dirname(src_build_file), get_buildroot())))

        git.add(test_file, test_build_file)
        git.commit('Introduce ClassTest.')

        yield
  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
Example #4
0
 def _create_tiny_git_repo(self):
   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.
     readme_file = os.path.join(worktree, 'README')
     touch(readme_file)
     # The contextmanager interface is only necessary if an explicit gitdir is not provided.
     with initialize_repo(worktree, gitdir=gitdir) as git:
       yield git, worktree, gitdir
Example #5
0
 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
Example #6
0
 def mk_project_tree(self, build_root_src):
   # Use mk_fs_tree only to feed the files for the git repo, not using its FileSystemProjectTree.
   worktree = self.mk_fs_tree(build_root_src).build_root
   with initialize_repo(worktree) as git_repo:
     yield ScmProjectTree(worktree, git_repo, 'HEAD')
Example #7
0
 def mk_project_tree(self, build_root_src):
     # Use mk_fs_tree only to feed the files for the git repo, not using its FileSystemProjectTree.
     worktree = self.mk_fs_tree(build_root_src).build_root
     with initialize_repo(worktree) as git_repo:
         yield ScmProjectTree(worktree, git_repo, 'HEAD')
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
Example #9
0
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."""
      write_path = os.path.join(worktree, path)
      with safe_open(write_path, 'w') as f:
        f.write(dedent(content))
      return write_path

    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
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:
    with safe_open(os.path.join(worktree, 'README'), 'w') as fp:
      fp.write('Just a test tree.')

    # Create an empty pants config file.
    touch(os.path.join(worktree, 'pants.ini'))

    # Copy .gitignore to new repo.
    shutil.copyfile('.gitignore', os.path.join(worktree, '.gitignore'))

    with initialize_repo(worktree=worktree, gitdir=os.path.join(worktree, '.git')) as git:
      # Resource File
      resource_file = os.path.join(worktree, 'src/resources/org/pantsbuild/resourceonly/README.md')
      with safe_open(resource_file, 'w') as fp:
        fp.write('Just resource.')

      resource_build_file = os.path.join(worktree, 'src/resources/org/pantsbuild/resourceonly/BUILD')
      with safe_open(resource_build_file, 'w') as fp:
        fp.write(dedent("""
        resources(
          name='resource',
          sources=['README.md'],
        )
        """))

      git.add(resource_file, resource_build_file)
      git.commit('Check in a resource target.')

      # Java Program
      src_file = os.path.join(worktree, 'src/java/org/pantsbuild/helloworld/helloworld.java')
      with safe_open(src_file, 'w') as fp:
        fp.write(dedent("""
        package org.pantsbuild.helloworld;

        class HelloWorld {
          public static void main(String[] args) {
            System.out.println("Hello, World!\n");
          }
        }
        """))

      src_build_file = os.path.join(worktree, 'src/java/org/pantsbuild/helloworld/BUILD')
      with safe_open(src_build_file, 'w') as fp:
        fp.write(dedent("""
        jvm_binary(
          dependencies=[
            '{}',
          ],
          source='helloworld.java',
          main='org.pantsbuild.helloworld.HelloWorld',
        )
        """.format('src/resources/org/pantsbuild/resourceonly:resource')))

      git.add(src_file, src_build_file)
      git.commit('hello world java program with a dependency on a resource file.')

      # Scala Program
      scala_src_dir = os.path.join(worktree, 'tests/scala/org/pantsbuild/cp-directories')
      safe_mkdir(os.path.dirname(scala_src_dir))
      shutil.copytree('testprojects/tests/scala/org/pantsbuild/testproject/cp-directories', scala_src_dir)
      git.add(scala_src_dir)
      git.commit('Check in a scala test target.')

      # Python library and binary
      python_src_dir = os.path.join(worktree, 'src/python/python_targets')
      safe_mkdir(os.path.dirname(python_src_dir))
      shutil.copytree('testprojects/src/python/python_targets', python_src_dir)
      git.add(python_src_dir)
      git.commit('Check in python targets.')

      # A `python_library` with `resources=['file.name']`.
      python_src_dir = os.path.join(worktree, 'src/python/sources')
      safe_mkdir(os.path.dirname(python_src_dir))
      shutil.copytree('testprojects/src/python/sources', python_src_dir)
      git.add(python_src_dir)
      git.commit('Check in a python library with resource dependency.')

      # Copy 3rdparty/BUILD.
      _3rdparty_build = os.path.join(worktree, '3rdparty/BUILD')
      safe_mkdir(os.path.dirname(_3rdparty_build))
      shutil.copyfile('3rdparty/BUILD', _3rdparty_build)
      git.add(_3rdparty_build)
      git.commit('Check in 3rdparty/BUILD.')

      with environment_as(PANTS_BUILDROOT_OVERRIDE=worktree):
        yield worktree
Example #11
0
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
    #          |--ClasspathDirectories.scala
    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.')

        # Create an empty pants config file.
        touch(os.path.join(worktree, 'pants.ini'))

        # Copy .gitignore to new repo.
        shutil.copyfile('.gitignore', os.path.join(worktree, '.gitignore'))

        with initialize_repo(worktree=worktree,
                             gitdir=os.path.join(worktree, '.git')) as git:
            # Resource File
            resource_file = os.path.join(
                worktree,
                'src/resources/org/pantsbuild/resourceonly/README.md')
            with safe_open(resource_file, 'w') as fp:
                fp.write('Just resource.')

            resource_build_file = os.path.join(
                worktree, 'src/resources/org/pantsbuild/resourceonly/BUILD')
            with safe_open(resource_build_file, 'w') as fp:
                fp.write(
                    dedent("""
        resources(
          name='resource',
          sources=['README.md'],
        )
        """))

            git.add(resource_file, resource_build_file)
            git.commit('Check in a resource target.')

            # Java Program
            src_file = os.path.join(
                worktree, 'src/java/org/pantsbuild/helloworld/helloworld.java')
            with safe_open(src_file, 'w') as fp:
                fp.write(
                    dedent("""
        package org.pantsbuild.helloworld;

        class HelloWorld {
          public static void main(String[] args) {
            System.out.println("Hello, World!\n");
          }
        }
        """))

            src_build_file = os.path.join(
                worktree, 'src/java/org/pantsbuild/helloworld/BUILD')
            with safe_open(src_build_file, 'w') as fp:
                fp.write(
                    dedent("""
        jvm_binary(
          dependencies=[
            '{}',
          ],
          source='helloworld.java',
          main='org.pantsbuild.helloworld.HelloWorld',
        )
        """.format('src/resources/org/pantsbuild/resourceonly:resource')))

            git.add(src_file, src_build_file)
            git.commit(
                'hello world java program with a dependency on a resource file.'
            )

            # Scala Program
            scala_src_dir = os.path.join(
                worktree, 'tests/scala/org/pantsbuild/cp-directories')
            safe_mkdir(os.path.dirname(scala_src_dir))
            shutil.copytree(
                'testprojects/tests/scala/org/pantsbuild/testproject/cp-directories',
                scala_src_dir)
            git.add(scala_src_dir)
            git.commit('Check in a scala test target.')

            # Python library and binary
            python_src_dir = os.path.join(worktree,
                                          'src/python/python_targets')
            safe_mkdir(os.path.dirname(python_src_dir))
            shutil.copytree('testprojects/src/python/python_targets',
                            python_src_dir)
            git.add(python_src_dir)
            git.commit('Check in python targets.')

            # A `python_library` with `resources=['file.name']`.
            python_src_dir = os.path.join(worktree, 'src/python/sources')
            safe_mkdir(os.path.dirname(python_src_dir))
            shutil.copytree('testprojects/src/python/sources', python_src_dir)
            git.add(python_src_dir)
            git.commit('Check in a python library with resource dependency.')

            # Copy 3rdparty/BUILD.
            _3rdparty_build = os.path.join(worktree, '3rdparty/BUILD')
            safe_mkdir(os.path.dirname(_3rdparty_build))
            shutil.copyfile('3rdparty/BUILD', _3rdparty_build)
            git.add(_3rdparty_build)
            git.commit('Check in 3rdparty/BUILD.')

            with environment_as(PANTS_BUILDROOT_OVERRIDE=worktree):
                yield worktree