コード例 #1
0
        def doTest(action=None):
            """Test the pre-commit hook for the given action."""
            content = "// Copyright (C) 1999, 2013"
            expected = "// Copyright (C) 1999,2013,%d" % YEAR

            with GitRepository() as repo:
                if action is not None:
                    repo.config(SECTION, KEY_ACTION, str(action))

                write(repo, "test.cpp", data=content)
                repo.add("test.cpp")

                regex = r"are not properly normalized"
                # By default (no action set), we implicitly use the fixup
                # action and correct any issues with the copyright header
                # directly.
                if action is None or action == Action.Fixup:
                    repo.commit()
                    self.assertEqual(read(repo, "test.cpp"), expected)
                elif action == Action.Check:
                    with self.assertRaisesRegex(ProcessError, regex):
                        repo.commit()
                elif action == Action.Warn:
                    _, err = repo.commit()
                    self.assertIn(b"are not properly normalized", err)
                else:
                    assert False, action
コード例 #2
0
    def testIgnore(self):
        """Verify that copyright.ignore setting is handled correctly."""
        with GitRepository() as repo:
            content = """\
        * Copyright (C) 2010,2011,2012,2013 foo                *
        * Copyright (C) 2013,2014 bar                          *
        * Copyright (C) 2013,2014 baz                          *
        * Copyright (C) 2010 Daniel Mueller ([email protected])  *
      """
            expected = """\
        * Copyright (C) 2010,2011,2012,2013 foo                *
        * Copyright (C) 2013,2014 bar                          *
        * Copyright (C) 2013-2014,%d baz                     *
        * Copyright (C) 2010 Daniel Mueller ([email protected])  *
      """ % YEAR
            repo.config(SECTION, KEY_IGNORE, r"foo", "--add")
            repo.config(SECTION, KEY_IGNORE, r"bar", "--add")
            repo.config(SECTION, KEY_IGNORE, r"deso@posteo\.[^ ]+", "--add")
            repo.config(SECTION, KEY_POLICY, r"pad")

            write(repo, "copyright.py", data=content)
            repo.add("copyright.py")
            repo.commit()

            new_content = read(repo, "copyright.py")
            self.assertEqual(new_content, expected)
コード例 #3
0
    def testInvalidPolicyIsComplainedAbout(self):
        """Verify that if an invalid/unsupported policy is set, we get an error."""
        with GitRepository() as repo:
            repo.config(SECTION, KEY_POLICY, "invalid")
            write(repo, "foo.cpp", data="")
            repo.add("foo.cpp")

            with self.assertRaises(ProcessError):
                repo.commit()
コード例 #4
0
 def _doMixinTest(self, repo_class):
     """Create a repository of the given class and create a commit."""
     # We simply instantiate the repository and create a commit. The
     # pre-commit hook will be run and it will fail in case the desired
     # environment variable is not inherited.
     with repo_class(GIT) as repo:
         write(repo, "foo", data="data")
         repo.add("foo")
         repo.commit()
コード例 #5
0
    def testSymlinks(self):
        """Verify that symbolic links are handled correctly."""
        with GitRepository() as repo:
            repo.config(SECTION, KEY_COPYRIGHT_REQUIRED, str(True))

            write(repo, "repo.dat", data="foobar")
            symlink(repo.path("repo.dat"), repo.path("repo.lnk"))

            repo.add("repo.lnk")
            repo.commit()
コード例 #6
0
  def testOutput(self):
    """Verify that we can retrieve a command's standard output contents."""
    with Repository(GIT) as foo:
      write(foo, "foo.c", data="// foo.c")
      foo.add("foo.c")
      foo.commit()

      # We also verify here that we can invoke a git command containing
      # a dash (rev-parse in this case).
      sha1, _ = foo.revParse("HEAD", stdout=b"")
      self.assertRegex(sha1[:-1].decode("utf-8"), "[0-9a-f]{40}")
コード例 #7
0
    def testSingleFileWithPadPolicy(self):
        """Verify that the commit hook can use the 'pad' policy."""
        with GitRepository() as repo:
            content = "* Copyright (C) 2010,2011,2012,2013 Daniel Mueller ([email protected])  *"
            expected = "* Copyright (C) 2010-2013,%d Daniel Mueller ([email protected])       *" % YEAR
            repo.config(SECTION, KEY_POLICY, "pad")
            write(repo, "test.py", data=content)
            repo.add("test.py")
            repo.commit()

            new_content = read(repo, "test.py")
            self.assertEqual(new_content, expected)
コード例 #8
0
    def testChangeReversion(self):
        """Verify that file change reversals are detected and files exluded from normalization."""
        with GitRepository() as repo:
            file_ = "some-file.txt"
            content1 = "# Copyright (c) 2013 All Right Reserved.\n"
            content2 = "# This is a comment!"
            expected1 = "# Copyright (c) 2013,%d All Right Reserved.\n" % YEAR

            write(repo, file_, data=content1)
            repo.add(file_)
            # We want to commit the unmodified content (i.e., without
            # copyright header normalization).
            repo.commit("--no-verify")

            write(repo, file_, data=content2, truncate=False)
            repo.add(file_)
            repo.commit()

            self.assertEqual(read(repo, file_), expected1 + content2)

            # Now we write back the previous content. Once we commit the
            # change the commit hook should detect that we effectively
            # reverted the file content and not apply any normalization.
            write(repo, file_, data=content1)
            repo.add(file_)
            # Add another file to the repository in order to not let the
            # commit become empty (that would not be a problem but it is very
            # unusual and not the case we want to test here).
            write(repo, "test.dat", data="# Copyright (c) %d." % YEAR)
            repo.add("test.dat")
            repo.commit("--amend")

            # The file must contain the original copyright header and not have
            # been normalized afterwards.
            self.assertEqual(read(repo, file_), content1)
コード例 #9
0
  def testAddCommitReset(self):
    """Test the add, commit, and reset functionality."""
    with Repository(GIT) as repo:
      write(repo, "file.dat", data="content")
      repo.add("file.dat")
      repo.commit()

      self.assertEqual(read(repo, "file.dat"), "content")

      write(repo, "file.dat", data="empty")
      self.assertEqual(read(repo, "file.dat"), "empty")

      repo.reset("HEAD", "--hard")
      self.assertEqual(read(repo, "file.dat"), "content")
コード例 #10
0
    def testSubmoduleHandling(self):
        """Verify that submodules are handled correctly."""
        with GitRepository() as lib,\
             GitRepository() as app:
            content = "// Copyright (c) 2013 All Right Reserved."
            expected = "// Copyright (c) 2013,%d All Right Reserved." % YEAR

            write(lib, "lib.dat", data=content)
            lib.add("lib.dat")
            lib.commit()

            app.submodule("add", lib.path())
            app.commit()

            self.assertEqual(read(lib, "lib.dat"), expected)
コード例 #11
0
        def doTest(content, fail, required=None):
            """Perform a single commit test and check for success or failure."""
            with GitRepository() as repo:
                if required is not None:
                    repo.config(SECTION, KEY_COPYRIGHT_REQUIRED, str(required))

                write(repo, "test.c", data=content)
                repo.add("test.c")

                if fail:
                    regex = r"No copyright header found"
                    with self.assertRaisesRegex(ProcessError, regex):
                        repo.commit()
                else:
                    repo.commit()
コード例 #12
0
  def testChdir(self):
    """Verify that we do not change the working directory if already in the git repo."""
    with Repository(GIT) as foo:
      cwd = getcwd()
      dir_ = foo.path("test")

      mkdir(dir_)
      write(foo, join(dir_, "test_file"), data="data")

      chdir(dir_)
      try:
        # Add a path relative to the current working directory.
        foo.add("test_file")
        foo.commit()
        self.assertEqual(getcwd(), dir_)
      finally:
        chdir(cwd)
コード例 #13
0
    def testSingleFileWithUnstagedChangesIsNormalized(self):
        """Verify that unstaged changes are handled correctly."""
        with GitRepository() as repo:
            content1 = "// Copyright (c) 2013 All Right Reserved."
            content2 = "// Copyright (c) 2013 All Right Reserved, deso."
            expected1 = "// Copyright (c) 2013,%d All Right Reserved, deso." % YEAR
            expected2 = "// Copyright (c) 2013,%d All Right Reserved." % YEAR
            write(repo, "test.c", data=content1)
            repo.add("test.c")
            write(repo, "test.c", data=content2)
            repo.commit()

            new_content = read(repo, "test.c")
            self.assertEqual(new_content, expected1)

            # The unstaged changes must not have been commited, so if we
            # discard them we should change the content of the file once
            # more.
            repo.reset("--hard")
            new_content = read(repo, "test.c")
            self.assertEqual(new_content, expected2)
コード例 #14
0
  def testRemote(self):
    """Test remote repository add and fetch."""
    with Repository(GIT) as lib,\
         Repository(GIT) as app:
      write(lib, "lib.py", data="# lib.py")
      lib.add("lib.py")
      lib.commit()

      app.remote("add", "--fetch", "lib", lib.path())
      app.checkout("lib/master")

      self.assertEqual(read(app, "lib.py"), read(lib, "lib.py"))

      # Create another commit in the library.
      write(lib, "other.dat", data="something else")
      lib.add("other.dat")
      lib.commit()

      app.fetch("lib")
      app.checkout("lib/master")

      self.assertEqual(read(app, "lib.py"), read(lib, "lib.py"))
      self.assertEqual(read(app, "other.dat"), read(lib, "other.dat"))
コード例 #15
0
    def testSingleFileIsNormalized(self):
        """Verify that a single file is normalized during commit."""
        with GitRepository() as repo:
            # We already verified that replacing works correctly in
            # principle so we do not required a fully blown file here
            # potentially covering all corner-cases but can just focus on
            # the copyright header only.

            # Note that we cannot monkey patch the current year retrieval to
            # return a dummy value because git (which invokes the commit hook)
            # runs in a separate process. Instead we use the copyright year
            # 2013 as the latest year in any of the tests. Since the module
            # was authored in 2015 whatever year we currently has must be
            # greater or equal to 2015 and hence result in a separation of the
            # years via comma (because only 2014 would be merged into the
            # span).
            content = "// Copyright (c) 2013 All Right Reserved."
            expected = "// Copyright (c) 2013,%d All Right Reserved." % YEAR
            write(repo, "test.c", data=content)
            repo.add("test.c")
            repo.commit()

            new_content = read(repo, "test.c")
            self.assertEqual(new_content, expected)