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)
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")
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)
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
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)
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)
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)
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)
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"))