mirror of
https://github.com/ivuorinen/gibidify.git
synced 2026-03-06 23:58:17 +00:00
feat: many features, check TODO.md
This commit is contained in:
@@ -7,8 +7,9 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
fileproc "github.com/ivuorinen/gibidify/fileproc"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/ivuorinen/gibidify/fileproc"
|
||||
)
|
||||
|
||||
func TestStartWriter_Formats(t *testing.T) {
|
||||
@@ -18,107 +19,109 @@ func TestStartWriter_Formats(t *testing.T) {
|
||||
format string
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
name: "JSON format",
|
||||
format: "json",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "YAML format",
|
||||
format: "yaml",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Markdown format",
|
||||
format: "markdown",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Invalid format",
|
||||
format: "invalid",
|
||||
expectError: true,
|
||||
},
|
||||
{"JSON format", "json", false},
|
||||
{"YAML format", "yaml", false},
|
||||
{"Markdown format", "markdown", false},
|
||||
{"Invalid format", "invalid", true},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
outFile, err := os.CreateTemp("", "gibidify_test_output")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp file: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := outFile.Close(); err != nil {
|
||||
t.Errorf("close temp file: %v", err)
|
||||
}
|
||||
if err := os.Remove(outFile.Name()); err != nil {
|
||||
t.Errorf("remove temp file: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Prepare channels
|
||||
writeCh := make(chan fileproc.WriteRequest, 2)
|
||||
doneCh := make(chan struct{})
|
||||
|
||||
// Write a couple of sample requests
|
||||
writeCh <- fileproc.WriteRequest{Path: "sample.go", Content: "package main"}
|
||||
writeCh <- fileproc.WriteRequest{Path: "example.py", Content: "def foo(): pass"}
|
||||
close(writeCh)
|
||||
|
||||
// Start the writer
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
fileproc.StartWriter(outFile, writeCh, doneCh, tc.format, "PREFIX", "SUFFIX")
|
||||
}()
|
||||
|
||||
// Wait until writer signals completion
|
||||
wg.Wait()
|
||||
<-doneCh // make sure all writes finished
|
||||
|
||||
// Read output
|
||||
data, err := os.ReadFile(outFile.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading output file: %v", err)
|
||||
}
|
||||
|
||||
data := runWriterTest(t, tc.format)
|
||||
if tc.expectError {
|
||||
// For an invalid format, we expect StartWriter to log an error
|
||||
// and produce no content or minimal content. There's no official
|
||||
// error returned, so check if it's empty or obviously incorrect.
|
||||
if len(data) != 0 {
|
||||
t.Errorf("Expected no output for invalid format, got:\n%s", data)
|
||||
}
|
||||
verifyErrorOutput(t, data)
|
||||
} else {
|
||||
// Valid format: check basic properties in the output
|
||||
content := string(data)
|
||||
switch tc.format {
|
||||
case "json":
|
||||
// Quick parse check
|
||||
var outStruct fileproc.OutputData
|
||||
if err := json.Unmarshal(data, &outStruct); err != nil {
|
||||
t.Errorf("JSON unmarshal failed: %v", err)
|
||||
}
|
||||
case "yaml":
|
||||
var outStruct fileproc.OutputData
|
||||
if err := yaml.Unmarshal(data, &outStruct); err != nil {
|
||||
t.Errorf("YAML unmarshal failed: %v", err)
|
||||
}
|
||||
case "markdown":
|
||||
// Check presence of code fences or "## File: ..."
|
||||
if !strings.Contains(content, "```") {
|
||||
t.Error("Expected markdown code fences not found")
|
||||
}
|
||||
}
|
||||
|
||||
// Prefix and suffix checks (common to JSON, YAML, markdown)
|
||||
if !strings.Contains(string(data), "PREFIX") {
|
||||
t.Errorf("Missing prefix in output: %s", data)
|
||||
}
|
||||
if !strings.Contains(string(data), "SUFFIX") {
|
||||
t.Errorf("Missing suffix in output: %s", data)
|
||||
}
|
||||
verifyValidOutput(t, data, tc.format)
|
||||
verifyPrefixSuffix(t, data)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// runWriterTest executes the writer with the given format and returns the output data.
|
||||
func runWriterTest(t *testing.T, format string) []byte {
|
||||
t.Helper()
|
||||
outFile, err := os.CreateTemp(t.TempDir(), "gibidify_test_output")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp file: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
if closeErr := outFile.Close(); closeErr != nil {
|
||||
t.Errorf("close temp file: %v", closeErr)
|
||||
}
|
||||
if removeErr := os.Remove(outFile.Name()); removeErr != nil {
|
||||
t.Errorf("remove temp file: %v", removeErr)
|
||||
}
|
||||
}()
|
||||
|
||||
// Prepare channels
|
||||
writeCh := make(chan fileproc.WriteRequest, 2)
|
||||
doneCh := make(chan struct{})
|
||||
|
||||
// Write a couple of sample requests
|
||||
writeCh <- fileproc.WriteRequest{Path: "sample.go", Content: "package main"}
|
||||
writeCh <- fileproc.WriteRequest{Path: "example.py", Content: "def foo(): pass"}
|
||||
close(writeCh)
|
||||
|
||||
// Start the writer
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
fileproc.StartWriter(outFile, writeCh, doneCh, format, "PREFIX", "SUFFIX")
|
||||
}()
|
||||
|
||||
// Wait until writer signals completion
|
||||
wg.Wait()
|
||||
<-doneCh // make sure all writes finished
|
||||
|
||||
// Read output
|
||||
data, err := os.ReadFile(outFile.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading output file: %v", err)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// verifyErrorOutput checks that error cases produce no output.
|
||||
func verifyErrorOutput(t *testing.T, data []byte) {
|
||||
t.Helper()
|
||||
if len(data) != 0 {
|
||||
t.Errorf("Expected no output for invalid format, got:\n%s", data)
|
||||
}
|
||||
}
|
||||
|
||||
// verifyValidOutput checks format-specific output validity.
|
||||
func verifyValidOutput(t *testing.T, data []byte, format string) {
|
||||
t.Helper()
|
||||
content := string(data)
|
||||
switch format {
|
||||
case "json":
|
||||
var outStruct fileproc.OutputData
|
||||
if err := json.Unmarshal(data, &outStruct); err != nil {
|
||||
t.Errorf("JSON unmarshal failed: %v", err)
|
||||
}
|
||||
case "yaml":
|
||||
var outStruct fileproc.OutputData
|
||||
if err := yaml.Unmarshal(data, &outStruct); err != nil {
|
||||
t.Errorf("YAML unmarshal failed: %v", err)
|
||||
}
|
||||
case "markdown":
|
||||
if !strings.Contains(content, "```") {
|
||||
t.Error("Expected markdown code fences not found")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// verifyPrefixSuffix checks that output contains expected prefix and suffix.
|
||||
func verifyPrefixSuffix(t *testing.T, data []byte) {
|
||||
t.Helper()
|
||||
content := string(data)
|
||||
if !strings.Contains(content, "PREFIX") {
|
||||
t.Errorf("Missing prefix in output: %s", data)
|
||||
}
|
||||
if !strings.Contains(content, "SUFFIX") {
|
||||
t.Errorf("Missing suffix in output: %s", data)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user