github.com/please-build/go-rules/tools/please_go@v0.0.0-20240319165128-ea27d6f5caba/generate/gomoddeps/gomoddeps_test.go (about) 1 package gomoddeps 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 var hostGoModPath = "tools/please_go/generate/gomoddeps/test_data/host_go_mod" 10 var moduleGoModPath = "tools/please_go/generate/gomoddeps/test_data/module_go_mod" 11 var invalidGoModPath = "tools/please_go/generate/gomoddeps/test_data/invalid_go_mod" 12 13 func TestErrors(t *testing.T) { 14 t.Run("errors if host go.mod does not exist", func(t *testing.T) { 15 deps, replacements, err := GetCombinedDepsAndReplacements("/does/not/exist", "/does/not/matter") 16 assert.Error(t, err) 17 assert.Nil(t, deps) 18 assert.Nil(t, replacements) 19 }) 20 21 t.Run("does not error if module go.mod does not exist", func(t *testing.T) { 22 deps, replacements, err := GetCombinedDepsAndReplacements(hostGoModPath, "/does/not/exist") 23 assert.NoError(t, err) 24 assert.NotNil(t, deps) 25 assert.NotNil(t, replacements) 26 }) 27 28 t.Run("errors if host go.mod is invalid", func(t *testing.T) { 29 deps, replacements, err := GetCombinedDepsAndReplacements(invalidGoModPath, "/does/not/exist") 30 assert.Error(t, err) 31 assert.Nil(t, deps) 32 assert.Nil(t, replacements) 33 }) 34 35 t.Run("errors if module go.mod is invalid", func(t *testing.T) { 36 deps, replacements, err := GetCombinedDepsAndReplacements(hostGoModPath, invalidGoModPath) 37 assert.Error(t, err) 38 assert.Nil(t, deps) 39 assert.Nil(t, replacements) 40 }) 41 } 42 43 func TestHostOnlyDeps(t *testing.T) { 44 t.Run("returns all host dependencies", func(t *testing.T) { 45 deps, _, err := GetCombinedDepsAndReplacements(hostGoModPath, "/does/not/matter") 46 assert.NoError(t, err) 47 48 assert.Len(t, deps, 3) 49 assert.Equal(t, []string{"example.com/foo", "example.com/bar", "example.com/bob"}, deps) 50 }) 51 52 t.Run("returns all host replacements", func(t *testing.T) { 53 _, replacements, err := GetCombinedDepsAndReplacements(hostGoModPath, "/does/not/matter") 54 assert.NoError(t, err) 55 56 assert.Len(t, replacements, 1) 57 assert.Equal(t, map[string]string{"example.com/bob": "example.com/new-bob"}, replacements) 58 }) 59 } 60 61 func TestModuleOnlyDeps(t *testing.T) { 62 t.Run("returns all host dependencies", func(t *testing.T) { 63 deps, _, err := GetCombinedDepsAndReplacements("", moduleGoModPath) 64 assert.NoError(t, err) 65 66 assert.Len(t, deps, 2) 67 assert.Equal(t, []string{"example.com/dob", "example.com/bab"}, deps) 68 }) 69 70 t.Run("returns all host replacements", func(t *testing.T) { 71 _, replacements, err := GetCombinedDepsAndReplacements("", moduleGoModPath) 72 assert.NoError(t, err) 73 74 assert.Len(t, replacements, 1) 75 assert.Equal(t, map[string]string{"example.com/bab": "example.com/new-bab"}, replacements) 76 }) 77 } 78 79 func TestBothDeps(t *testing.T) { 80 t.Run("returns combined dependencies", func(t *testing.T) { 81 deps, _, err := GetCombinedDepsAndReplacements(hostGoModPath, moduleGoModPath) 82 assert.NoError(t, err) 83 84 assert.Len(t, deps, 5) 85 assert.Equal(t, []string{"example.com/foo", "example.com/bar", "example.com/bob", "example.com/dob", "example.com/bab"}, deps) 86 }) 87 88 t.Run("returns only host replacements", func(t *testing.T) { 89 _, replacements, err := GetCombinedDepsAndReplacements(hostGoModPath, moduleGoModPath) 90 assert.NoError(t, err) 91 92 assert.Len(t, replacements, 1) 93 assert.Equal(t, map[string]string{"example.com/bob": "example.com/new-bob"}, replacements) 94 }) 95 }