Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"os"
"path/filepath"
"reflect"
"runtime"
"testing"
)

func TestGetIgnoreList(t *testing.T) {
tmpFile, err := os.CreateTemp("", "gptignore")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())

content := "# comment\nlogs/\n\nassets/*.min.js\n"
if _, err := tmpFile.WriteString(content); err != nil {
t.Fatalf("failed to write temp file: %v", err)
}
tmpFile.Close()

list, err := getIgnoreList(tmpFile.Name())
if err != nil {
t.Fatalf("getIgnoreList returned error: %v", err)
}

expected := []string{"logs/**", "assets/*.min.js"}
if runtime.GOOS == "windows" {
expected = []string{"logs\\**", "assets\\*.min.js"}
}
if !reflect.DeepEqual(list, expected) {
t.Fatalf("expected %v, got %v", expected, list)
}
}

func TestShouldIgnore_DefaultAndNested(t *testing.T) {
cases := []struct {
path string
want bool
}{
{"node_modules", true},
{filepath.Join("node_modules", "pkg", "index.js"), true},
{filepath.Join("src", "node_modules", "index.js"), false},
}

for _, c := range cases {
if got := shouldIgnore(c.path, nil); got != c.want {
t.Errorf("shouldIgnore(%q) = %v, want %v", c.path, got, c.want)
}
}
}

func TestShouldIgnore_WithPatterns(t *testing.T) {
ignoreList := []string{"logs/**", "**/*.min.js", "docs/**/ignored/**"}
cases := []struct {
path string
want bool
}{
{filepath.Join("logs", "app.log"), true},
{filepath.Join("static", "js", "file.min.js"), true},
{filepath.Join("docs", "api", "ignored", "readme.md"), true},
{filepath.Join("docs", "api", "readme.md"), false},
}

for _, c := range cases {
if got := shouldIgnore(c.path, ignoreList); got != c.want {
t.Errorf("shouldIgnore(%q) = %v, want %v", c.path, got, c.want)
}
}
}