package validate import ( "strings" "testing" ) func TestContentValid(t *testing.T) { cases := []struct { name string content string fileType string valid bool }{ {"json ok", `{"a":1}`, "json", true}, {"json bad", `{`, "json", false}, {"yaml ok", "a: 1\nb: 2\n", "yaml", true}, {"yaml bad", "a: [1,\n", "yaml", false}, {"toml ok", "a = 1\n", "toml", true}, {"toml bad", "a = = 1\n", "toml", false}, {"xml ok", "", "xml", true}, {"xml bad", "", "xml", false}, {"ini ok", "[s]\nk = v\n", "ini", true}, {"ini bad", "no separator line\n", "ini", false}, {"env ok", "FOO=bar\nexport BAZ=1\n", "env", true}, {"env bad", "1BAD=x\n", "env", false}, {"dockerfile ok", "FROM alpine\nRUN echo hi\n", "dockerfile", true}, {"dockerfile bad", "FROM alpine\nBOGUS x\n", "dockerfile", false}, {"unknown type", "whatever", "nope", false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { r := Content(tc.content, tc.fileType) if tc.fileType == "nope" { if r != nil { t.Fatalf("unknown type must return nil, got %+v", r) } return } if r == nil { t.Fatal("nil result") } if r.Valid != tc.valid { t.Errorf("Valid = %v, want %v (errors=%v)", r.Valid, tc.valid, r.Errors) } }) } } func TestDetectFileType(t *testing.T) { cases := map[string]string{ "a.json": "json", "dir/b.yaml": "yaml", "c.YML": "yaml", "Dockerfile": "dockerfile", "path/to/Dockerfile": "dockerfile", ".env": "env", ".env.local": "env", "conf/app.conf": "ini", "x.txt": "", } for in, want := range cases { if got := DetectFileType(in); got != want { t.Errorf("DetectFileType(%q) = %q, want %q", in, got, want) } } } func TestFormatResult(t *testing.T) { ok := &Result{Valid: true, FileType: "json"} if got := ok.FormatResult("/a.json"); !strings.HasPrefix(got, "✓") { t.Errorf("valid format = %q", got) } bad := &Result{FileType: "json", Errors: []string{"boom"}} if got := bad.FormatResult("/a.json"); !strings.HasPrefix(got, "✗") { t.Errorf("invalid format = %q", got) } }