github.com/purpleclay/gitz@v0.8.2-0.20240515052600-43f80eea2fe1/config_test.go (about) 1 package git_test 2 3 import ( 4 "testing" 5 6 git "github.com/purpleclay/gitz" 7 "github.com/purpleclay/gitz/gittest" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestConfig(t *testing.T) { 13 gittest.InitRepository(t) 14 gittest.ConfigSet(t, "user.name", "joker", "user.email", "joker@dc.com") 15 16 client, _ := git.NewClient() 17 cfg, err := client.Config() 18 19 require.NoError(t, err) 20 assert.Equal(t, "joker", cfg["user.name"]) 21 assert.Equal(t, "joker@dc.com", cfg["user.email"]) 22 } 23 24 func TestConfigOnlyLatestValues(t *testing.T) { 25 gittest.InitRepository(t) 26 gittest.ConfigSet(t, "user.name", "joker", "user.name", "scarecrow") 27 28 client, _ := git.NewClient() 29 cfg, err := client.Config() 30 31 require.NoError(t, err) 32 assert.Equal(t, "scarecrow", cfg["user.name"]) 33 } 34 35 func TestConfigL(t *testing.T) { 36 gittest.InitRepository(t) 37 gittest.ConfigSet(t, "user.name", "alfred") 38 39 client, _ := git.NewClient() 40 cfg, err := client.ConfigL("user.name", "user.email") 41 42 require.NoError(t, err) 43 require.Len(t, cfg["user.name"], 2) 44 assert.Equal(t, "alfred", cfg["user.name"][0]) 45 assert.Equal(t, gittest.DefaultAuthorName, cfg["user.name"][1]) 46 47 require.Len(t, cfg["user.email"], 1) 48 assert.Equal(t, gittest.DefaultAuthorEmail, cfg["user.email"][0]) 49 } 50 51 func configEquals(t *testing.T, path, expected string) { 52 t.Helper() 53 cfg, err := gittest.Exec(t, "git config --get "+path) 54 55 require.NoError(t, err) 56 assert.Equal(t, expected, cfg) 57 } 58 59 func TestConfigSetL(t *testing.T) { 60 gittest.InitRepository(t) 61 62 client, _ := git.NewClient() 63 err := client.ConfigSetL("user.phobia", "bats", "user.birth.place", "gotham") 64 65 require.NoError(t, err) 66 configEquals(t, "user.phobia", "bats") 67 configEquals(t, "user.birth.place", "gotham") 68 } 69 70 func TestConfigSetLMismatchedPairsError(t *testing.T) { 71 gittest.InitRepository(t) 72 73 client, _ := git.NewClient() 74 err := client.ConfigSetL("user.hobbies") 75 76 assert.EqualError(t, err, "config paths mismatch. path: user.hobbies is missing a corresponding value") 77 } 78 79 func TestConfigSetLNothingSetIfError(t *testing.T) { 80 gittest.InitRepository(t) 81 82 client, _ := git.NewClient() 83 err := client.ConfigSetL("user.hobbies", "fighting crime", "user.arch.3nemy", "joker") 84 85 require.Error(t, err) 86 configMissing(t, "user.hobbies") 87 configMissing(t, "user.4rch.enemy") 88 } 89 90 func configMissing(t *testing.T, path string) { 91 t.Helper() 92 cfg, err := gittest.Exec(t, "git config --get "+path) 93 94 require.Error(t, err) 95 require.Empty(t, cfg) 96 } 97 98 func TestCheckConfigPathError(t *testing.T) { 99 tests := []struct { 100 name string 101 path string 102 errMsg string 103 }{ 104 { 105 name: "InvalidMissingDot", 106 path: "nodot", 107 errMsg: "path: nodot invalid as dot separator is missing or is the last character", 108 }, 109 { 110 name: "InvalidJustSection", 111 path: "section.only.", 112 errMsg: "path: section.only|.| invalid as dot separator is missing or is the last character", 113 }, 114 { 115 name: "InvalidDigitAfterLastDot", 116 path: "a.bad.4pple", 117 errMsg: "path: a.bad.|4|pple invalid as first character after final dot must be a letter [a-zA-Z]", 118 }, 119 { 120 name: "InvalidContainsNonAlphanumeric", 121 path: "no.$symbol.allowed", 122 errMsg: "path: no.|$|symbol.allowed invalid as non alphanumeric character detected [a-zA-Z0-9]", 123 }, 124 } 125 for _, tt := range tests { 126 t.Run(tt.name, func(t *testing.T) { 127 require.EqualError(t, git.CheckConfigPath(tt.path), tt.errMsg) 128 }) 129 } 130 } 131 132 func TestToInlineConfig(t *testing.T) { 133 cfg, err := git.ToInlineConfig("user.name", "penguin", "user.email", "penguin@dc.com") 134 135 require.NoError(t, err) 136 assert.ElementsMatch(t, []string{"-c user.name='penguin'", "-c user.email='penguin@dc.com'"}, cfg) 137 }