|
|
|
@@ -0,0 +1,102 @@
|
|
|
|
|
package processor
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"flag"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/carlosarraes/shush/internal/config"
|
|
|
|
|
"github.com/carlosarraes/shush/internal/git"
|
|
|
|
|
"github.com/carlosarraes/shush/internal/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var update = flag.Bool("update", false, "update golden files")
|
|
|
|
|
|
|
|
|
|
type wholeFileCase struct {
|
|
|
|
|
name string
|
|
|
|
|
file string // fixture under testdata/, e.g. "py_inline.py"
|
|
|
|
|
cli types.CLI
|
|
|
|
|
cfg *config.Config // nil => config.Default()
|
|
|
|
|
gitMode bool // true => processFileWithLineRanges (surgical git path)
|
|
|
|
|
ranges []git.LineRange // used only when gitMode; nil => whole file
|
|
|
|
|
wantBackup bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func runWholeFileCase(t *testing.T, tc wholeFileCase) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
src := filepath.Join("testdata", tc.file)
|
|
|
|
|
in, err := os.ReadFile(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("read input %s: %v", src, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tmp := t.TempDir()
|
|
|
|
|
work := filepath.Join(tmp, tc.file)
|
|
|
|
|
if err := os.WriteFile(work, in, 0644); err != nil {
|
|
|
|
|
t.Fatalf("write work copy: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg := tc.cfg
|
|
|
|
|
if cfg == nil {
|
|
|
|
|
cfg = config.Default()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p := &Processor{cli: tc.cli}
|
|
|
|
|
if tc.gitMode {
|
|
|
|
|
err = p.processFileWithLineRanges(work, tc.ranges, cfg)
|
|
|
|
|
} else {
|
|
|
|
|
lang, derr := DetectLanguage(work)
|
|
|
|
|
if derr != nil {
|
|
|
|
|
t.Fatalf("detect language for %s: %v", work, derr)
|
|
|
|
|
}
|
|
|
|
|
err = p.processFileInMemory(work, lang, cfg)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("process: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
got, err := os.ReadFile(work)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("read result: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
golden := src + ".golden"
|
|
|
|
|
if *update {
|
|
|
|
|
if err := os.WriteFile(golden, got, 0644); err != nil {
|
|
|
|
|
t.Fatalf("update golden: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
want, err := os.ReadFile(golden)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("read golden %s: %v", golden, err)
|
|
|
|
|
}
|
|
|
|
|
if !bytes.Equal(got, want) {
|
|
|
|
|
t.Errorf("output mismatch:\n--- got ---\n%q\n--- want ---\n%q", got, want)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if tc.wantBackup {
|
|
|
|
|
bakData, err := os.ReadFile(work + ".bak")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("read backup: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if !bytes.Equal(bakData, in) {
|
|
|
|
|
t.Errorf("backup mismatch:\n--- got ---\n%q\n--- want ---\n%q", bakData, in)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestWholeFile(t *testing.T) {
|
|
|
|
|
cases := []wholeFileCase{
|
|
|
|
|
{name: "python comment-only line removed", file: "py_comment_only.py"},
|
|
|
|
|
{name: "python inline comment stripped", file: "py_inline.py"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
runWholeFileCase(t, tc)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|