package util import ( "errors" "path/filepath" "testing" "code.thetadev.de/TSGRain/WebUI/src/fixtures" "github.com/stretchr/testify/assert" ) func TestDoesFileExist(t *testing.T) { fixtures.CdProjectRoot() type args struct { filepath string } tests := []struct { name string args args want bool }{ { name: "exist", args: args{"go.mod"}, want: true, }, { name: "not_exist", args: args{"banana.mod"}, want: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := DoesFileExist(tt.args.filepath); got != tt.want { t.Errorf("DoesFileExist() = %v, want %v", got, tt.want) } }) } } func TestFindFile(t *testing.T) { testfiles := fixtures.GetTestfilesDir() //nolint:lll tests := []struct { name string explicitPath string locations []string endings []string expect string expectErr error }{ { name: "locations", explicitPath: "", locations: []string{filepath.Join(testfiles, "tsgrain_web")}, endings: []string{"yml", "toml"}, expect: filepath.Join(testfiles, "tsgrain_web.toml"), expectErr: nil, }, { name: "locations_nf", explicitPath: "", locations: []string{filepath.Join(testfiles, "banana")}, endings: []string{"yml", "toml"}, expect: "", expectErr: errors.New("none of the following files found: src/fixtures/testfiles/banana.yml; src/fixtures/testfiles/banana.toml"), }, { name: "no_endings", explicitPath: "", locations: []string{filepath.Join(testfiles, "banana"), filepath.Join(testfiles, "htpasswd")}, endings: nil, expect: filepath.Join(testfiles, "htpasswd"), expectErr: nil, }, { name: "no_endings_nf", explicitPath: "", locations: []string{filepath.Join(testfiles, "banana"), filepath.Join(testfiles, "apple")}, endings: nil, expect: "", expectErr: errors.New("none of the following files found: src/fixtures/testfiles/banana; src/fixtures/testfiles/apple"), }, { name: "explicit", explicitPath: filepath.Join(testfiles, "tsgrain_web.toml"), locations: []string{filepath.Join(testfiles, "banana")}, endings: []string{"yml", "toml"}, expect: filepath.Join(testfiles, "tsgrain_web.toml"), expectErr: nil, }, { name: "explicit_nf", explicitPath: filepath.Join(testfiles, "banana.toml"), locations: []string{filepath.Join(testfiles, "banana")}, endings: []string{"yml", "toml"}, expect: "", expectErr: errors.New("file src/fixtures/testfiles/banana.toml not found"), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { fpath, err := FindFile(tt.explicitPath, tt.locations, tt.endings) assert.Equal(t, tt.expectErr, err) assert.Equal(t, tt.expect, fpath) }) } }