From d49408267689d9fd57ff6fe556e33a628c4a8527 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Thu, 1 May 2025 03:49:44 +0100 Subject: [PATCH] add list/create tests + testdata --- cmd/gignore/create_test.go | 60 +++++++++++++++++++++++++++++++ cmd/gignore/list_test.go | 43 ++++++++++++++++++++++ cmd/gignore/testdata/go.gitignore | 26 ++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 cmd/gignore/create_test.go create mode 100644 cmd/gignore/list_test.go create mode 100644 cmd/gignore/testdata/go.gitignore diff --git a/cmd/gignore/create_test.go b/cmd/gignore/create_test.go new file mode 100644 index 0000000..5ba7dd9 --- /dev/null +++ b/cmd/gignore/create_test.go @@ -0,0 +1,60 @@ +package main + +import ( + "bytes" + "context" + "io" + "os" + "testing" + + "github.com/onyx-and-iris/gignore" +) + +// mockFileWriter is a mock implementation of the gignore.FileWriter interface. +// It writes the content to an io.Writer and adds a header and footer. +// This is used for testing purposes to avoid writing to the actual file system. +type mockFileWriter struct { + out io.Writer + TargetFileName string +} + +// Write implements the io.Writer interface for the mockFileWriter. +func (m mockFileWriter) Write(content []byte) (int, error) { + const header = "# Auto-generated .gitignore by gignore: github.com/onyx-and-iris/gignore\n" + const footer = "\n# End of gignore: github.com/onyx-and-iris/gignore\n" + + m.out.Write([]byte(header)) + m.out.Write(content) + m.out.Write([]byte(footer)) + + return len(content) + len(header) + len(footer), nil +} + +func TestCreateSuccess(t *testing.T) { + client := gignore.New( + gignore.WithTemplateDirectory(gignore.DefaultTemplateDirectory), + ) + ctx := context.WithValue(context.Background(), clientKey, client) + var templateBuffer bytes.Buffer + client.Writer = mockFileWriter{out: &templateBuffer} + + var outBuffer bytes.Buffer + err := createTemplate(ctx, &outBuffer, "go") + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + expectedContent, err := os.ReadFile("testdata/go.gitignore") + if err != nil { + t.Fatalf("failed to read testdata/go.gitignore: %v", err) + } + + if templateBuffer.String() != string(expectedContent) { + t.Fatalf("expected template content %q, got %q", string(expectedContent), templateBuffer.String()) + } + + expectedOutput := "√ created go .gitignore file\n" + if outBuffer.String() != expectedOutput { + t.Fatalf("expected %q, got %q", expectedOutput, outBuffer.String()) + } +} diff --git a/cmd/gignore/list_test.go b/cmd/gignore/list_test.go new file mode 100644 index 0000000..ecd630f --- /dev/null +++ b/cmd/gignore/list_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "bytes" + "context" + "testing" + + "github.com/onyx-and-iris/gignore" +) + +func TestListSuccess(t *testing.T) { + client := gignore.New( + gignore.WithTemplateDirectory(gignore.DefaultTemplateDirectory), + ) + ctx := context.WithValue(context.Background(), clientKey, client) + + testCases := []struct { + language string + expected string + }{ + { + language: "python", + expected: "templates/gitignoreio/circuitpython.gitignore\n" + + "templates/gitignoreio/python.gitignore\n" + + "templates/gitignoreio/pythonvanilla.gitignore\n", + }, + } + + var out bytes.Buffer + + // Call the listTemplates function with the context and output buffer + err := listTemplates(ctx, &out, "python") + if err != nil { + t.Fatalf("listTemplates failed: %v", err) + } + + // Check if the output contains the expected templates + for _, tc := range testCases { + if out.String() != tc.expected { + t.Errorf("Expected %q, got %q", tc.expected, out.String()) + } + } +} diff --git a/cmd/gignore/testdata/go.gitignore b/cmd/gignore/testdata/go.gitignore new file mode 100644 index 0000000..07c51ea --- /dev/null +++ b/cmd/gignore/testdata/go.gitignore @@ -0,0 +1,26 @@ +# Auto-generated .gitignore by gignore: github.com/onyx-and-iris/gignore + +### Go ### +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +# End of gignore: github.com/onyx-and-iris/gignore