Skip to content

chore: ensure user's settings don't impact tests

Ash McKenzie requested to merge ashmckenzie/fix-git-tag-tests into main

Description

When running make test locally, I discovered some settings in ~/.gitconfig were causing test failures, specifically the following configuration:

[tag]
  sort = -v:refname

This MR explicitly sets the HOME environment variable to a non-existent directory just in the context of the tests by using t.Setenv("HOME", "/tmp/non-existent-user").

This technique effectively tells git to look for a .gitconfig file at /tmp/non-existent-user/.gitconfig which doesn't exist, resulting in no local config settings are applied. There will be other places where local user config could interfere with tests but for now, I only noticed this one.

cc @jay_mccure

Related Issues

N/A

How has this been tested?

Running make test should work as before, but if you wanted to replicate the issue, add the following into your ~/.gitconfig file:

[tag]
  sort = -v:refname

and re-run make test from the main branch to see the issue:

$ make test
--snip--
=== Failed
=== FAIL: pkg/git TestListTags/multiple_non-semver_tag (0.23s)
    git_test.go:523:
        	Error Trace:	/Users/ash/src/gitlab/cli/pkg/git/git_test.go:523
        	Error:      	Not equal:
        	            	expected: []string{"a", "b"}
        	            	actual  : []string{"b", "a"}

        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1,4 +1,4 @@
        	            	 ([]string) (len=2) {
        	            	- (string) (len=1) "a",
        	            	- (string) (len=1) "b"
        	            	+ (string) (len=1) "b",
        	            	+ (string) (len=1) "a"
        	            	 }
        	Test:       	TestListTags/multiple_non-semver_tag

=== FAIL: pkg/git TestListTags/multiple_semver_tags (0.28s)
    git_test.go:523:
        	Error Trace:	/Users/ash/src/gitlab/cli/pkg/git/git_test.go:523
        	Error:      	Not equal:
        	            	expected: []string{"1.0.0", "2.0.0", "3.0.0"}
        	            	actual  : []string{"3.0.0", "2.0.0", "1.0.0"}

        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1,5 +1,5 @@
        	            	 ([]string) (len=3) {
        	            	- (string) (len=5) "1.0.0",
        	            	+ (string) (len=5) "3.0.0",
        	            	  (string) (len=5) "2.0.0",
        	            	- (string) (len=5) "3.0.0"
        	            	+ (string) (len=5) "1.0.0"
        	            	 }
        	Test:       	TestListTags/multiple_semver_tags

=== FAIL: pkg/git TestListTags/multiple_semver_tags_with_extra_newlines (0.28s)
    git_test.go:523:
        	Error Trace:	/Users/ash/src/gitlab/cli/pkg/git/git_test.go:523
        	Error:      	Not equal:
        	            	expected: []string{"1.0.0", "2.0.0", "3.0.0"}
        	            	actual  : []string{"3.0.0", "2.0.0", "1.0.0"}

        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1,5 +1,5 @@
        	            	 ([]string) (len=3) {
        	            	- (string) (len=5) "1.0.0",
        	            	+ (string) (len=5) "3.0.0",
        	            	  (string) (len=5) "2.0.0",
        	            	- (string) (len=5) "3.0.0"
        	            	+ (string) (len=5) "1.0.0"
        	            	 }
        	Test:       	TestListTags/multiple_semver_tags_with_extra_newlines

=== FAIL: pkg/git TestListTags (1.83s)

DONE 1443 tests, 7 skipped, 4 failures in 42.324s
make: *** [test] Error 1

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation
  • Chore (Related to CI or Packaging to platforms)
  • Test gap
Edited by Ash McKenzie

Merge request reports