Exemplo n.º 1
0
    def test_verbosity(self):
        self._create_simple_commit(
            u"WIP: Thïs is a title.\nContënt on the second line")
        output = gitlint("-v",
                         _cwd=self.tmp_git_repo,
                         _tty_in=True,
                         _ok_code=[3])
        expected = u"1: T3\n1: T5\n2: B4\n"
        self.assertEqualStdout(output, expected)

        output = gitlint("-vv",
                         _cwd=self.tmp_git_repo,
                         _tty_in=True,
                         _ok_code=[3])
        self.assertEqualStdout(
            output, self.get_expected("test_config/test_verbosity_1"))

        output = gitlint("-vvv",
                         _cwd=self.tmp_git_repo,
                         _tty_in=True,
                         _ok_code=[3])
        self.assertEqualStdout(
            output, self.get_expected("test_config/test_verbosity_2"))

        # test silent mode
        output = gitlint("--silent",
                         _cwd=self.tmp_git_repo,
                         _tty_in=True,
                         _ok_code=[3])
        self.assertEqualStdout(output, "")
Exemplo n.º 2
0
    def test_verbosity(self):
        self._create_simple_commit(
            u"WIP: Thïs is a title.\nContënt on the second line")
        output = gitlint("-v",
                         _cwd=self.tmp_git_repo,
                         _tty_in=True,
                         _ok_code=[3])
        expected = u"1: T3\n1: T5\n2: B4\n"
        self.assertEqual(output, expected)

        output = gitlint("-vv",
                         _cwd=self.tmp_git_repo,
                         _tty_in=True,
                         _ok_code=[3])
        expected = u"1: T3 Title has trailing punctuation (.)\n" + \
                   u"1: T5 Title contains the word 'WIP' (case-insensitive)\n" + \
                   u"2: B4 Second line is not empty\n"
        self.assertEqual(output, expected)

        output = gitlint("-vvv",
                         _cwd=self.tmp_git_repo,
                         _tty_in=True,
                         _ok_code=[3])
        expected = u"1: T3 Title has trailing punctuation (.): \"WIP: Thïs is a title.\"\n" + \
                   u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: Thïs is a title.\"\n" + \
                   u"2: B4 Second line is not empty: \"Contënt on the second line\"\n"
        self.assertEqual(output, expected)

        # test silent mode
        output = gitlint("--silent",
                         _cwd=self.tmp_git_repo,
                         _tty_in=True,
                         _ok_code=[3])
        self.assertEqual(output, "")
Exemplo n.º 3
0
    def test_commit_hook_worktree(self):
        """ Tests that hook installation and un-installation also work in git worktrees.
        Test steps:
            ```sh
            git init <tmpdir>
            cd <tmpdir>
            git worktree add <worktree-tempdir>
            cd <worktree-tempdir>
            gitlint install-hook
            gitlint uninstall-hook
            ```
        """
        tmp_git_repo = self.create_tmp_git_repo()
        self._create_simple_commit(u"Simple title\n\nContënt in the body",
                                   git_repo=tmp_git_repo)

        worktree_dir = self.generate_temp_path()
        self.tmp_git_repos.append(
            worktree_dir)  # make sure we clean up the worktree afterwards

        git("worktree", "add", worktree_dir, _cwd=tmp_git_repo, _tty_in=True)

        output_installed = gitlint("install-hook", _cwd=worktree_dir)
        expected_hook_path = os.path.join(tmp_git_repo, ".git", "hooks",
                                          "commit-msg")
        expected_msg = "Successfully installed gitlint commit-msg hook in {0}\n".format(
            expected_hook_path)
        self.assertEqual(output_installed, expected_msg)

        output_uninstalled = gitlint("uninstall-hook", _cwd=worktree_dir)
        expected_hook_path = os.path.join(tmp_git_repo, ".git", "hooks",
                                          "commit-msg")
        expected_msg = "Successfully uninstalled gitlint commit-msg hook from {0}\n".format(
            expected_hook_path)
        self.assertEqual(output_uninstalled, expected_msg)
Exemplo n.º 4
0
    def test_verbosity(self):
        self._create_simple_commit(
            "WIP: This is a title.\nContent on the second line")
        output = gitlint(
            "-v", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3])
        expected = "1: T3\n1: T5\n2: B4\n"
        self.assertEqual(output, expected)

        output = gitlint(
            "-vv", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3])
        expected = "1: T3 Title has trailing punctuation (.)\n" + \
                   "1: T5 Title contains the word 'WIP' (case-insensitive)\n" + \
                   "2: B4 Second line is not empty\n"
        self.assertEqual(output, expected)

        output = gitlint(
            "-vvv", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3])
        expected = "1: T3 Title has trailing punctuation (.): \"WIP: This is a title.\"\n" + \
                   "1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: This is a title.\"\n" + \
                   "2: B4 Second line is not empty: \"Content on the second line\"\n"
        self.assertEqual(output, expected)

        # test silent mode
        output = gitlint(
            "--silent", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3])
        self.assertEqual(output, "")
Exemplo n.º 5
0
 def test_set_rule_option(self):
     self._create_simple_commit("This is a title.")
     output = gitlint("-c", "title-max-length.line-length=5", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3])
     expected = "1: T1 Title exceeds max length (16>5): \"This is a title.\"\n" + \
                "1: T3 Title has trailing punctuation (.): \"This is a title.\"\n" + \
                "3: B6 Body message is missing\n"
     self.assertEqual(output, expected)
Exemplo n.º 6
0
    def test_violations(self):
        """ Test linting multiple commits with violations """
        git("checkout",
            "-b",
            "test-branch-commits-violations-base",
            _cwd=self.tmp_git_repo)
        self._create_simple_commit(u"Sïmple title.\n")
        git("checkout",
            "-b",
            "test-branch-commits-violations",
            _cwd=self.tmp_git_repo)

        self._create_simple_commit(u"Sïmple title2.\n")
        commit_sha1 = self.get_last_commit_hash()[:10]
        self._create_simple_commit(u"Sïmple title3.\n")
        commit_sha2 = self.get_last_commit_hash()[:10]
        output = gitlint(
            "--commits",
            "test-branch-commits-violations-base...test-branch-commits-violations",
            _cwd=self.tmp_git_repo,
            _tty_in=True,
            _ok_code=[4])
        expected = (
            u"Commit {0}:\n".format(commit_sha2) +
            u"1: T3 Title has trailing punctuation (.): \"Sïmple title3.\"\n" +
            u"3: B6 Body message is missing\n"
            "\n"
            u"Commit {0}:\n".format(commit_sha1) +
            u"1: T3 Title has trailing punctuation (.): \"Sïmple title2.\"\n"
            u"3: B6 Body message is missing\n")

        self.assertEqual(output.exit_code, 4)
        self.assertEqual(output, expected)
Exemplo n.º 7
0
    def test_ignore_commits(self):
        """ Tests multiple commits of which some rules get igonored because of ignore-* rules """
        # Create repo and some commits
        tmp_git_repo = self.create_tmp_git_repo()
        self._create_simple_commit(u"Sïmple title.\n\nSimple bödy describing the commit", git_repo=tmp_git_repo)
        # Normally, this commit will give T3 (trailing-punctuation), T5 (WIP) and B5 (bod-too-short) violations
        # But in this case only B5 because T3 and T5 are being ignored because of config
        self._create_simple_commit(u"Release: WIP tïtle.\n\nShort", git_repo=tmp_git_repo)
        # In the following 2 commits, the T3 violations are as normal
        self._create_simple_commit(
            u"Sïmple WIP title3.\n\nThis is \ta relëase commit\nMore info", git_repo=tmp_git_repo)
        self._create_simple_commit(u"Sïmple title4.\n\nSimple bödy describing the commit4", git_repo=tmp_git_repo)
        revlist = git("rev-list", "HEAD", _tty_in=True, _cwd=tmp_git_repo).split()

        config_path = self.get_sample_path("config/ignore-release-commits")
        output = gitlint("--commits", "HEAD", "--config", config_path, _cwd=tmp_git_repo, _tty_in=True, _ok_code=[4])

        expected = (
            u"Commit {0}:\n".format(revlist[0][:10]) +
            u"1: T3 Title has trailing punctuation (.): \"Sïmple title4.\"\n\n" +
            u"Commit {0}:\n".format(revlist[1][:10]) +
            u"1: T5 Title contains the word 'WIP' (case-insensitive): \"Sïmple WIP title3.\"\n\n" +
            u"Commit {0}:\n".format(revlist[2][:10]) +
            u"3: B5 Body message is too short (5<20): \"Short\"\n\n" +
            u"Commit {0}:\n".format(revlist[3][:10]) +
            u"1: T3 Title has trailing punctuation (.): \"Sïmple title.\"\n"
        )

        self.assertEqualStdout(output, expected)
Exemplo n.º 8
0
    def test_lint_head(self):
        """ Testing whether we can also recognize special refs like 'HEAD' """
        tmp_git_repo = self.create_tmp_git_repo()
        self._create_simple_commit(
            u"Sïmple title.\n\nSimple bödy describing the commit",
            git_repo=tmp_git_repo)
        self._create_simple_commit(u"Sïmple title", git_repo=tmp_git_repo)
        self._create_simple_commit(
            u"WIP: Sïmple title\n\nSimple bödy describing the commit",
            git_repo=tmp_git_repo)
        output = gitlint("--commits",
                         "HEAD",
                         _cwd=tmp_git_repo,
                         _tty_in=True,
                         _ok_code=[3])
        revlist = git("rev-list", "HEAD", _tty_in=True,
                      _cwd=tmp_git_repo).split()

        expected = (
            u"Commit {0}:\n".format(revlist[0][:10]) +
            u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: Sïmple title\"\n\n"
            + u"Commit {0}:\n".format(revlist[1][:10]) +
            u"3: B6 Body message is missing\n\n" +
            u"Commit {0}:\n".format(revlist[2][:10]) +
            u"1: T3 Title has trailing punctuation (.): \"Sïmple title.\"\n")

        self.assertEqual(output, expected)
Exemplo n.º 9
0
 def test_set_rule_option(self):
     self._create_simple_commit("This is a title.")
     output = gitlint("-c", "title-max-length.line-length=5", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3])
     expected = "1: T1 Title exceeds max length (16>5): \"This is a title.\"\n" + \
                "1: T3 Title has trailing punctuation (.): \"This is a title.\"\n" + \
                "3: B6 Body message is missing\n"
     self.assertEqual(output, expected)
Exemplo n.º 10
0
    def test_pipe_input(self):
        error_msg = None
        # For some odd reason, sh doesn't return the error output when piping something into gitlint.
        # Note that this does work as expected in the test_errors testcase.
        # To work around this we raise and catch an exception
        try:
            gitlint(echo(u"WIP: Pïpe test."), _tty_in=False)
        except ErrorReturnCode as e:
            # StdErr is returned as bytes -> decode to unicode string
            # http://stackoverflow.com/questions/606191/convert-bytes-to-a-python-string
            error_msg = e.stderr.decode(DEFAULT_ENCODING)

        expected = u"1: T3 Title has trailing punctuation (.): \"WIP: Pïpe test.\"\n" + \
                   u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: Pïpe test.\"\n" + \
                   "3: B6 Body message is missing\n"

        self.assertEqual(error_msg, expected)
Exemplo n.º 11
0
 def test_errors(self):
     commit_msg = "WIP: This is a title.\nContent on the second line"
     self._create_simple_commit(commit_msg)
     output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3])
     expected = "1: T3 Title has trailing punctuation (.): \"WIP: This is a title.\"\n" + \
                "1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: This is a title.\"\n" + \
                "2: B4 Second line is not empty: \"Content on the second line\"\n"
     self.assertEqual(output, expected)
Exemplo n.º 12
0
 def test_errors(self):
     commit_msg = "WIP: This is a title.\nContent on the second line"
     self._create_simple_commit(commit_msg)
     output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3])
     expected = "1: T3 Title has trailing punctuation (.): \"WIP: This is a title.\"\n" + \
                "1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: This is a title.\"\n" + \
                "2: B4 Second line is not empty: \"Content on the second line\"\n"
     self.assertEqual(output, expected)
Exemplo n.º 13
0
    def test_pipe_input(self):
        error_msg = None
        # For some odd reason, sh doesn't return the error output when piping something into gitlint.
        # Note that this does work as expected in the test_errors testcase.
        # To work around this we raise and catch an exception
        try:
            gitlint(echo("WIP: Pipe test."), _tty_in=False)
        except ErrorReturnCode as e:
            # StdErr is returned as bytes -> decode to unicode string
            # http://stackoverflow.com/questions/606191/convert-bytes-to-a-python-string
            error_msg = e.stderr.decode("utf-8")

        expected = "1: T3 Title has trailing punctuation (.): \"WIP: Pipe test.\"\n" + \
                   "1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: Pipe test.\"\n" + \
                   "3: B6 Body message is missing\n"

        self.assertEqual(error_msg, expected)
Exemplo n.º 14
0
 def test_successful(self):
     # Test for STDIN with and without a TTY attached
     self._create_simple_commit(
         u"Sïmple title\n\nSimple bödy describing the commit")
     output = gitlint(_cwd=self.tmp_git_repo,
                      _tty_in=True,
                      _err_to_out=True)
     self.assertEqual(output, "")
Exemplo n.º 15
0
 def test_msg_filename(self):
     tmp_commit_msg_file = self.create_tmpfile("WIP: msg-fïlename test.")
     output = gitlint("--msg-filename",
                      tmp_commit_msg_file,
                      _tty_in=True,
                      _ok_code=[3])
     self.assertEqualStdout(
         output, self.get_expected("test_gitlint/test_msg_filename_1"))
Exemplo n.º 16
0
    def test_revert_commit(self):
        self._create_simple_commit(u"WIP: Cömmit on master.\n\nSimple bödy")
        hash = self.get_last_commit_hash()
        git("revert", hash, _cwd=self.tmp_git_repo)

        # Run gitlint and assert output is empty
        output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True)
        self.assertEqualStdout(output, "")

        # Assert that we do see the error if we disable the ignore-revert-commits option
        output = gitlint("-c",
                         "general.ignore-revert-commits=false",
                         _cwd=self.tmp_git_repo,
                         _tty_in=True,
                         _ok_code=[1])
        self.assertEqual(output.exit_code, 1)
        expected = u"1: T5 Title contains the word 'WIP' (case-insensitive): \"Revert \"WIP: Cömmit on master.\"\"\n"
        self.assertEqualStdout(output, expected)
Exemplo n.º 17
0
 def test_set_rule_option(self):
     self._create_simple_commit(u"This ïs a title.")
     output = gitlint("-c",
                      "title-max-length.line-length=5",
                      _tty_in=True,
                      _cwd=self.tmp_git_repo,
                      _ok_code=[3])
     self.assertEqualStdout(
         output, self.get_expected("test_config/test_set_rule_option_1"))
Exemplo n.º 18
0
 def test_user_defined_rules(self):
     extra_path = self.get_example_path()
     commit_msg = u"WIP: Thi$ is å title\nContent on the second line"
     self._create_simple_commit(commit_msg)
     output = gitlint("--extra-path", extra_path, _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[4])
     expected = u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: Thi$ is å title\"\n" + \
                "1: UC2 Body does not contain a 'Signed-Off-By' line\n" + \
                u"1: UL1 Title contains the special character '$': \"WIP: Thi$ is å title\"\n" + \
                "2: B4 Second line is not empty: \"Content on the second line\"\n"
     self.assertEqualStdout(output, expected)
Exemplo n.º 19
0
 def test_ignore_by_id(self):
     self._create_simple_commit(
         u"WIP: Thïs is a title.\nContënt on the second line")
     output = gitlint("--ignore",
                      "T5,B4",
                      _tty_in=True,
                      _cwd=self.tmp_git_repo,
                      _ok_code=[1])
     expected = u"1: T3 Title has trailing punctuation (.): \"WIP: Thïs is a title.\"\n"
     self.assertEqual(output, expected)
Exemplo n.º 20
0
 def test_ignore_by_name(self):
     self._create_simple_commit(
         u"WIP: Thïs is a title.\nContënt on the second line")
     output = gitlint("--ignore",
                      "title-must-not-contain-word,body-first-line-empty",
                      _cwd=self.tmp_git_repo,
                      _tty_in=True,
                      _ok_code=[1])
     expected = u"1: T3 Title has trailing punctuation (.): \"WIP: Thïs is a title.\"\n"
     self.assertEqual(output, expected)
Exemplo n.º 21
0
    def setUp(self):
        self.responses = []
        self.response_index = 0
        self.githook_output = []

        # install git commit-msg hook and assert output
        output_installed = gitlint("install-hook", _cwd=self.tmp_git_repo)
        expected_installed = u"Successfully installed gitlint commit-msg hook in %s/.git/hooks/commit-msg\n" % \
                             self.tmp_git_repo
        self.assertEqualStdout(output_installed, expected_installed)
Exemplo n.º 22
0
 def test_successful(self):
     """ Test linting multiple commits without violations """
     git("checkout", "-b", "test-branch-commits-base", _cwd=self.tmp_git_repo)
     self._create_simple_commit(u"Sïmple title\n\nSimple bödy describing the commit")
     git("checkout", "-b", "test-branch-commits", _cwd=self.tmp_git_repo)
     self._create_simple_commit(u"Sïmple title2\n\nSimple bödy describing the commit2")
     self._create_simple_commit(u"Sïmple title3\n\nSimple bödy describing the commit3")
     output = gitlint("--commits", "test-branch-commits-base...test-branch-commits",
                      _cwd=self.tmp_git_repo, _tty_in=True)
     self.assertEqualStdout(output, "")
Exemplo n.º 23
0
    def setUp(self):
        self.responses = []
        self.response_index = 0
        self.githook_output = []

        # install git commit-msg hook and assert output
        output_installed = gitlint("install-hook", _cwd=self.tmp_git_repo)
        expected_installed = "Successfully installed gitlint commit-msg hook in %s/.git/hooks/commit-msg\n" % \
                             self.tmp_git_repo
        self.assertEqual(output_installed, expected_installed)
Exemplo n.º 24
0
 def test_invalid_contrib_rules(self):
     self._create_simple_commit("WIP: test")
     output = gitlint("--contrib",
                      u"föobar,CC1",
                      _cwd=self.tmp_git_repo,
                      _tty_in=True,
                      _ok_code=[255])
     self.assertEqualStdout(
         output,
         u"Config Error: No contrib rule with id or name 'föobar' found.\n")
Exemplo n.º 25
0
 def test_user_defined_rules(self):
     extra_path = self.get_example_path()
     commit_msg = "WIP: Thi$ is a title\nContent on the second line"
     self._create_simple_commit(commit_msg)
     output = gitlint("--extra-path", extra_path, _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[4])
     expected = "1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: Thi$ is a title\"\n" + \
                "1: UC2 Body does not contain a 'Signed-Off-By' line\n" + \
                "1: UL1 Title contains the special character '$': \"WIP: Thi$ is a title\"\n" + \
                "2: B4 Second line is not empty: \"Content on the second line\"\n"
     self.assertEqual(output, expected)
Exemplo n.º 26
0
    def test_squash_commit(self):
        # Create a normal commit and assert that it has a violation
        test_filename = self._create_simple_commit(
            u"Cömmit on WIP master\n\nSimple bödy that is long enough")
        output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[1])
        expected = u"1: T5 Title contains the word 'WIP' (case-insensitive): \"Cömmit on WIP master\"\n"
        self.assertEqualStdout(output, expected)

        # Make a small modification to the commit and commit it using squash commit
        with io.open(os.path.join(self.tmp_git_repo, test_filename),
                     "a",
                     encoding=DEFAULT_ENCODING) as fh:
            # Wanted to write a unicode string, but that's obnoxious if you want to do it across Python 2 and 3.
            # https://stackoverflow.com/questions/22392377/
            # error-writing-a-file-with-file-write-in-python-unicodeencodeerror
            # So just keeping it simple - ASCII will here
            fh.write(u"Appending some stuff\n")

        git("add", test_filename, _cwd=self.tmp_git_repo)

        git("commit",
            "--squash",
            self.get_last_commit_hash(),
            "-m",
            u"Töo short body",
            _cwd=self.tmp_git_repo)

        # Assert that gitlint does not show an error for the fixup commit
        output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True)
        # No need to check exit code, the command above throws an exception on > 0 exit codes
        self.assertEqualStdout(output, "")

        # Make sure that if we set the ignore-squash-commits option to false that we do still see the violations
        output = gitlint("-c",
                         "general.ignore-squash-commits=false",
                         _cwd=self.tmp_git_repo,
                         _tty_in=True,
                         _ok_code=[2])
        expected = u"1: T5 Title contains the word 'WIP' (case-insensitive): \"squash! Cömmit on WIP master\"\n" + \
            u"3: B5 Body message is too short (14<20): \"Töo short body\"\n"

        self.assertEqualStdout(output, expected)
Exemplo n.º 27
0
 def test_ignore_by_name(self):
     self._create_simple_commit(
         "WIP: This is a title.\nContent on the second line")
     output = gitlint(
         "--ignore",
         "title-must-not-contain-word,body-first-line-empty",
         _cwd=self.tmp_git_repo,
         _tty_in=True,
         _ok_code=[1])
     expected = "1: T3 Title has trailing punctuation (.): \"WIP: This is a title.\"\n"
     self.assertEqual(output, expected)
Exemplo n.º 28
0
 def test_lint_single_commit(self):
     self._create_simple_commit(u"Sïmple title.\n")
     self._create_simple_commit(u"Sïmple title2.\n")
     commit_sha = self.get_last_commit_hash()
     refspec = "{0}^...{0}".format(commit_sha)
     self._create_simple_commit(u"Sïmple title3.\n")
     output = gitlint("--commits", refspec, _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[2])
     expected = (u"1: T3 Title has trailing punctuation (.): \"Sïmple title2.\"\n" +
                 u"3: B6 Body message is missing\n")
     self.assertEqual(output.exit_code, 2)
     self.assertEqualStdout(output, expected)
Exemplo n.º 29
0
 def test_contrib_rules(self):
     self._create_simple_commit(
         u"WIP Thi$ is å title\n\nMy bödy that is a bit longer than 20 chars"
     )
     output = gitlint("--contrib",
                      "contrib-title-conventional-commits,CC1",
                      _cwd=self.tmp_git_repo,
                      _tty_in=True,
                      _ok_code=[4])
     self.assertEqualStdout(
         output, self.get_expected("test_contrib/test_contrib_rules_1"))
Exemplo n.º 30
0
 def test_ignore_by_code(self):
     self._create_simple_commit(
         "WIP: This is a title.\nContent on the second line")
     output = gitlint(
         "--ignore",
         "T5,B4",
         _cwd=self.tmp_git_repo,
         _tty_in=True,
         _ok_code=[1])
     expected = "1: T3 Title has trailing punctuation (.): \"WIP: This is a title.\"\n"
     self.assertEqual(output, expected)
Exemplo n.º 31
0
    def test_commit_hook(self):
        # install git commit-msg hook
        output_installed = gitlint("install-hook", _cwd=self.tmp_git_repo)
        expected_installed = "Successfully installed gitlint commit-msg hook in %s/.git/hooks/commit-msg\n" % \
                             self.tmp_git_repo

        githook_output = []

        # callback function that captures git commit-msg hook output
        def interact(line, stdin):
            githook_output.append(line)
            # Answer 'yes' to question to keep violating commit-msg
            if "Your commit message contains the above violations" in line:
                stdin.put("y\n")

        test_filename = self._create_simple_commit("WIP: This is a title.\nContent on the second line", out=interact)
        # Determine short commit-msg hash, needed to determine expected output
        short_hash = git("rev-parse", "--short", "HEAD", _cwd=self.tmp_git_repo, _tty_in=True).replace("\n", "")

        expected_output = ['gitlint: checking commit message...\n',
                           '1: T3 Title has trailing punctuation (.): "WIP: This is a title."\n',
                           '1: T5 Title contains the word \'WIP\' (case-insensitive): "WIP: This is a title."\n',
                           '2: B4 Second line is not empty: "Content on the second line"\n',
                           '3: B6 Body message is missing\n',
                           '-----------------------------------------------\n',
                           'gitlint: \x1b[31mYour commit message contains the above violations.\x1b[0m\n',
                           'Continue with commit anyways (this keeps the current commit message)? [y/n] ' +
                           '[master (root-commit) %s] WIP: This is a title. Content on the second line\n' % short_hash,
                           ' 1 file changed, 0 insertions(+), 0 deletions(-)\n',
                           ' create mode 100644 %s\n' % test_filename]

        # uninstall git commit-msg hook
        output_uninstalled = gitlint("uninstall-hook", _cwd=self.tmp_git_repo)
        expected_uninstalled = "Successfully uninstalled gitlint commit-msg hook from %s/.git/hooks/commit-msg\n" % \
                               self.tmp_git_repo

        # Do all assertaions at the end so that the uninstall happens even if we have an assertion that doesn't
        # work out. This doesn't block the other tests.
        self.assertEqual(output_installed, expected_installed)
        self.assertListEqual(expected_output, githook_output)
        self.assertEqual(output_uninstalled, expected_uninstalled)
Exemplo n.º 32
0
 def test_config_from_file(self):
     commit_msg = u"WIP: Thïs is a title thåt is a bit longer.\nContent on the second line\n" + \
                  "This line of the body is here because we need it"
     self._create_simple_commit(commit_msg)
     config_path = self.get_sample_path("config/gitlintconfig")
     output = gitlint("--config",
                      config_path,
                      _cwd=self.tmp_git_repo,
                      _tty_in=True,
                      _ok_code=[5])
     self.assertEqualStdout(
         output, self.get_expected("test_config/test_config_from_file_1"))
Exemplo n.º 33
0
 def test_invalid_user_defined_rules(self):
     extra_path = self.get_sample_path("user_rules/incorrect_linerule")
     self._create_simple_commit("WIP: test")
     output = gitlint("--extra-path",
                      extra_path,
                      _cwd=self.tmp_git_repo,
                      _tty_in=True,
                      _ok_code=[255])
     self.assertEqual(
         output,
         "Config Error: User-defined rule class 'MyUserLineRule' must have a 'validate' method\n"
     )
Exemplo n.º 34
0
 def test_user_defined_rules(self):
     extra_path = self.get_example_path()
     commit_msg = u"WIP: Thi$ is å title\nContent on the second line"
     self._create_simple_commit(commit_msg)
     output = gitlint("--extra-path",
                      extra_path,
                      _cwd=self.tmp_git_repo,
                      _tty_in=True,
                      _ok_code=[4])
     self.assertEqualStdout(
         output,
         self.get_expected("test_user_defined/test_user_defined_rules_1"))
Exemplo n.º 35
0
    def test_config_from_file_debug(self):
        commit_msg = "WIP: This is a title that is a bit longer.\nContent on the second line\n" + \
                     "This line of the body is here because we need it"
        self._create_simple_commit(commit_msg)
        config_path = self.get_sample_path("config/gitlintconfig")
        output = gitlint("--config", config_path, "--debug", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[4])

        expected = self.get_expected('debug_output1', {'config_path': config_path, 'target': self.tmp_git_repo})

        # TODO(jroovers): test for trailing whitespace -> git automatically strips whitespace when passing
        # taking a commit message via 'git commit -m'
        self.assertEqual(output, expected)
Exemplo n.º 36
0
 def test_user_defined_rules_with_config(self):
     # TODO(jroovers): This test is expected to fail. This is because we have a problem with gitlint config parsing
     # We will likely need to rewrite how config is taken into account before applying the config.
     extra_path = self.get_example_path()
     commit_msg = "WIP: Thi$ is a title\nContent on the second line"
     self._create_simple_commit(commit_msg)
     output = gitlint("--extra-path", extra_path, "-c", "body-max-line-count.max-line-count=1",
                      _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[4])
     expected = "1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: Thi$ is a title\"\n" + \
                "1: UC2 Body does not contain a 'Signed-Off-By' line\n" + \
                "1: UL1 Title contains the special character '$': \"WIP: Thi$ is a title\"\n" + \
                "2: B4 Second line is not empty: \"Content on the second line\"\n"
     self.assertEqual(output, expected)
Exemplo n.º 37
0
    def test_lint_head(self):
        """ Testing whether we can also recognize special refs like 'HEAD' """
        tmp_git_repo = self.create_tmp_git_repo()
        self._create_simple_commit(u"Sïmple title.\n\nSimple bödy describing the commit", git_repo=tmp_git_repo)
        self._create_simple_commit(u"Sïmple title", git_repo=tmp_git_repo)
        self._create_simple_commit(u"WIP: Sïmple title\n\nSimple bödy describing the commit", git_repo=tmp_git_repo)
        output = gitlint("--commits", "HEAD", _cwd=tmp_git_repo, _tty_in=True, _ok_code=[3])
        revlist = git("rev-list", "HEAD", _tty_in=True, _cwd=tmp_git_repo).split()

        expected_kwargs = {"commit_sha0": revlist[0][:10], "commit_sha1": revlist[1][:10],
                           "commit_sha2": revlist[2][:10]}

        self.assertEqualStdout(output, self.get_expected("test_commits/test_lint_head_1", expected_kwargs))
Exemplo n.º 38
0
    def test_msg_filename(self):
        tmp_commit_msg_file = self.create_tmpfile("WIP: msg-fïlename test.")

        output = gitlint("--msg-filename",
                         tmp_commit_msg_file,
                         _tty_in=True,
                         _ok_code=[3])

        expected = u"1: T3 Title has trailing punctuation (.): \"WIP: msg-fïlename test.\"\n" + \
            u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: msg-fïlename test.\"\n" + \
            u"3: B6 Body message is missing\n"

        self.assertEqual(output, expected)
Exemplo n.º 39
0
    def test_pipe_input(self):
        # NOTE: There is no use in testing this with _tty_in=True, because if you pipe something into a command
        # there never is a TTY connected to stdin (per definition).
        output = gitlint(echo(u"WIP: Pïpe test."),
                         _tty_in=False,
                         _err_to_out=True,
                         _ok_code=[3])

        expected = u"1: T3 Title has trailing punctuation (.): \"WIP: Pïpe test.\"\n" + \
                   u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: Pïpe test.\"\n" + \
                   u"3: B6 Body message is missing\n"

        self.assertEqual(output, expected)
Exemplo n.º 40
0
    def test_successful_gitconfig(self):
        """ Test gitlint when the underlying repo has specific git config set.
        In the past, we've had issues with gitlint failing on some of these, so this acts as a regression test. """

        # Different commentchar (Note: tried setting this to a special unicode char, but git doesn't like that)
        git("config", "--add", "core.commentchar", "$", _cwd=self.tmp_git_repo)
        self._create_simple_commit(
            u"Sïmple title\n\nSimple bödy describing the commit\n$after commentchar\t ignored"
        )
        output = gitlint(_cwd=self.tmp_git_repo,
                         _tty_in=True,
                         _err_to_out=True)
        self.assertEqualStdout(output, "")
Exemplo n.º 41
0
    def test_successful_merge_commit(self):
        # Create branch on master
        self._create_simple_commit("Commit on master\n\nSimple body")

        # Create test branch, add a commit and determine the commit hash
        git("checkout", "-b", "test-branch", _cwd=self.tmp_git_repo)
        git("checkout", "test-branch", _cwd=self.tmp_git_repo)
        commit_title = "Commit on test-branch with a pretty long title that will cause issues when merging"
        self._create_simple_commit("{0}\n\nSimple body".format(commit_title))
        hash = git(
            "rev-parse", "HEAD", _cwd=self.tmp_git_repo, _tty_in=True).replace(
                "\n", "")

        # Checkout master and merge the commit
        # We explicitly set the title of the merge commit to the title of the previous commit as this or similar
        # behavior is what many tools do that handle merges (like github, gerrit, etc).
        git("checkout", "master", _cwd=self.tmp_git_repo)
        git("merge",
            "--no-ff",
            "-m",
            "Merge '{0}'".format(commit_title),
            hash,
            _cwd=self.tmp_git_repo)

        # Run gitlint and assert output is empty
        output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True)
        self.assertEqual(output, "")

        # Assert that we do see the error if we disable the ignore-merge-commits option
        output = gitlint(
            "-c",
            "general.ignore-merge-commits=false",
            _cwd=self.tmp_git_repo,
            _tty_in=True,
            _ok_code=[1])
        self.assertEqual(
            output,
            "1: T1 Title exceeds max length (90>72): \"Merge '{0}'\"\n".format(
                commit_title))
Exemplo n.º 42
0
    def test_successful_merge_commit(self):
        # Create branch on master
        self._create_simple_commit(u"Cömmit on master\n\nSimple bödy")

        # Create test branch, add a commit and determine the commit hash
        git("checkout", "-b", "test-branch", _cwd=self.tmp_git_repo)
        git("checkout", "test-branch", _cwd=self.tmp_git_repo)
        commit_title = u"Commit on test-brånch with a pretty long title that will cause issues when merging"
        self._create_simple_commit(u"{0}\n\nSïmple body".format(commit_title))
        hash = git("rev-parse", "HEAD", _cwd=self.tmp_git_repo,
                   _tty_in=True).replace("\n", "")

        # Checkout master and merge the commit
        # We explicitly set the title of the merge commit to the title of the previous commit as this or similar
        # behavior is what many tools do that handle merges (like github, gerrit, etc).
        git("checkout", "master", _cwd=self.tmp_git_repo)
        git("merge",
            "--no-ff",
            "-m",
            u"Merge '{0}'".format(commit_title),
            hash,
            _cwd=self.tmp_git_repo)

        # Run gitlint and assert output is empty
        output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True)
        self.assertEqual(output, "")

        # Assert that we do see the error if we disable the ignore-merge-commits option
        output = gitlint("-c",
                         "general.ignore-merge-commits=false",
                         _cwd=self.tmp_git_repo,
                         _tty_in=True,
                         _ok_code=[1])
        self.assertEqual(output.exit_code, 1)
        self.assertEqual(
            output,
            u"1: T1 Title exceeds max length (90>72): \"Merge '{0}'\"\n".
            format(commit_title))
Exemplo n.º 43
0
    def test_config_from_file(self):
        commit_msg = "WIP: This is a title that is a bit longer.\nContent on the second line\n" + \
                     "This line of the body is here because we need it"
        self._create_simple_commit(commit_msg)
        config_path = self.get_sample_path("config/gitlintconfig")
        output = gitlint("--config", config_path, _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[4])

        expected = "1: T1 Title exceeds max length (42>20)\n" + \
                   "1: T5 Title contains the word 'WIP' (case-insensitive)\n" + \
                   "2: B4 Second line is not empty\n" + \
                   "3: B1 Line exceeds max length (48>30)\n"

        # TODO(jroovers): test for trailing whitespace -> git automatically strips whitespace when passing
        # taking a commit message via 'git commit -m'

        self.assertEqual(output, expected)
Exemplo n.º 44
0
 def test_successful(self):
     self._create_simple_commit("Simple title\n\nSimple body")
     output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True)
     self.assertEqual(output, "")
Exemplo n.º 45
0
 def tearDown(self):
     # uninstall git commit-msg hook and assert output
     output_uninstalled = gitlint("uninstall-hook", _cwd=self.tmp_git_repo)
     expected_uninstalled = "Successfully uninstalled gitlint commit-msg hook from %s/.git/hooks/commit-msg\n" % \
                            self.tmp_git_repo
     self.assertEqual(output_uninstalled, expected_uninstalled)