add list/create tests + testdata

This commit is contained in:
onyx-and-iris 2025-05-01 03:49:44 +01:00
parent 09360bab6e
commit d494082676
3 changed files with 129 additions and 0 deletions

View File

@ -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())
}
}

43
cmd/gignore/list_test.go Normal file
View File

@ -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())
}
}
}

26
cmd/gignore/testdata/go.gitignore vendored Normal file
View File

@ -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