Repository navigation
Expand file tree
/
Copy pathcode_fix_test.go
More file actions
113 lines (107 loc) · 4.03 KB
/
Copy pathcode_fix_test.go
File metadata and controls
113 lines (107 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"context"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
codev0 "github.com/codefly-dev/core/generated/go/codefly/services/code/v0"
)
func TestParseESLintFixUsesFixedOutputAndDiagnostics(t *testing.T) {
data := []byte(`[{"output":"const value = 1;\n","messages":[{"ruleId":"no-unused-vars","severity":2,"message":"unused","line":1,"column":7}]}]`)
result, err := parseESLintFix(data, []byte("const value=1\n"))
if err != nil {
t.Fatal(err)
}
if string(result.Content) != "const value = 1;\n" || len(result.Diagnostics) != 1 || !strings.Contains(result.Diagnostics[0], "no-unused-vars") {
t.Fatalf("parsed result = %+v", result)
}
}
func TestNextFixerUsesProjectLocalESLintWithoutWritingOnDryRun(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("test fixture uses a POSIX shell")
}
dir := t.TempDir()
binDir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"scripts":{"lint":"eslint"},"devDependencies":{"eslint":"^9"}}`), 0o644); err != nil {
t.Fatal(err)
}
original := "const value=1\n"
if err := os.WriteFile(filepath.Join(dir, "sample.ts"), []byte(original), 0o644); err != nil {
t.Fatal(err)
}
fakeNPM := "#!/bin/sh\ncat >/dev/null\nprintf '%s' '[{\"output\":\"const value = 1;\\n\",\"messages\":[]}]'\n"
if err := os.WriteFile(filepath.Join(binDir, "npm"), []byte(fakeNPM), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
svc := NewService()
svc.sourceLocation = dir
server := NewCode(svc)
response, err := server.Execute(context.Background(), &codev0.CodeRequest{Operation: &codev0.CodeRequest_Fix{Fix: &codev0.FixRequest{
File: "sample.ts", DryRun: true,
}}})
if err != nil {
t.Fatal(err)
}
fix := response.GetFix()
if !fix.GetSuccess() || !fix.GetChanged() || fix.GetWrote() || fix.GetContent() != "const value = 1;\n" {
t.Fatalf("Next fixer = %+v failure=%+v", fix, response.GetFailure())
}
written, _ := os.ReadFile(filepath.Join(dir, "sample.ts"))
if string(written) != original {
t.Fatalf("dry-run changed source: %q", written)
}
}
func TestNextFixerRejectsSuccessfulEmptyBiomeOutput(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("test fixture uses a POSIX shell")
}
dir := t.TempDir()
binDir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"devDependencies":{"@biomejs/biome":"^2"}}`), 0o644); err != nil {
t.Fatal(err)
}
original := "export const value = 1;\n"
if err := os.WriteFile(filepath.Join(dir, "sample.ts"), []byte(original), 0o644); err != nil {
t.Fatal(err)
}
// Reproduces the dangerous boundary condition: the tool consumes stdin,
// exits successfully, and writes no stdout.
fakeNPM := "#!/bin/sh\ncat >/dev/null\nexit 0\n"
if err := os.WriteFile(filepath.Join(binDir, "npm"), []byte(fakeNPM), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
svc := NewService()
svc.sourceLocation = dir
server := NewCode(svc)
response, err := server.Execute(context.Background(), &codev0.CodeRequest{Operation: &codev0.CodeRequest_Fix{Fix: &codev0.FixRequest{
File: "sample.ts", DryRun: true,
}}})
if err != nil {
t.Fatal(err)
}
if fix := response.GetFix(); fix != nil && fix.GetSuccess() {
t.Fatalf("empty output was accepted: %+v", fix)
}
if failure := response.GetFailure(); failure == nil || !strings.Contains(failure.GetMessage(), "refusing destructive fix") {
t.Fatalf("failure = %+v", failure)
}
written, _ := os.ReadFile(filepath.Join(dir, "sample.ts"))
if string(written) != original {
t.Fatalf("failed fix changed source: %q", written)
}
}
func TestRejectEmptySourceFixAllowsEmptyAndNonEmptyTransitions(t *testing.T) {
if err := rejectEmptySourceFix("test", nil, nil); err != nil {
t.Fatal(err)
}
if err := rejectEmptySourceFix("test", []byte("source"), []byte("fixed")); err != nil {
t.Fatal(err)
}
if err := rejectEmptySourceFix("test", []byte("source"), []byte(" \n")); err == nil {
t.Fatal("non-empty source to whitespace output was accepted")
}
}