File size: 2,493 Bytes
6932c8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package diffdetect

import "testing"

func TestInspect(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name    string
		content string
		want    Signal
	}{
		{
			name: "git unified diff",
			content: `diff --git a/main.go b/main.go
--- a/main.go
+++ b/main.go
@@ -1,2 +1,3 @@
 package main
+import "fmt"
`,
			want: Signal{HasHunk: true, HasFileHeader: true, HasGitHeader: true},
		},
		{
			name: "non-git unified diff",
			content: `--- old.c
+++ old.c
@@ -1 +1 @@
-old
+new
`,
			want: Signal{HasHunk: true, HasFileHeader: true, HasGitHeader: false},
		},
		{
			name:    "plain text",
			content: "hello world",
			want:    Signal{},
		},
		{
			name:    "hunk only",
			content: "@@ -1 +1 @@\n-old\n+new\n",
			want:    Signal{HasHunk: true},
		},
		{
			name:    "headers only",
			content: "--- a/file\n+++ b/file\n",
			want:    Signal{HasFileHeader: true},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()
			got := Inspect(tt.content)
			if got != tt.want {
				t.Errorf("Inspect() = %+v, want %+v", got, tt.want)
			}
		})
	}
}

func TestIsUnifiedDiff(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name    string
		content string
		want    bool
	}{
		{
			name: "github-style multi-file diff",
			content: `diff --git a/one.txt b/one.txt
--- a/one.txt
+++ b/one.txt
@@ -1 +1 @@
-a
+b
diff --git a/two.txt b/two.txt
--- a/two.txt
+++ b/two.txt
@@ -1 +1 @@
-c
+d
`,
			want: true,
		},
		{
			name: "non-git unified patch",
			content: `--- a/old.c
+++ b/old.c
@@ -1,3 +1,4 @@
 #include <stdio.h>
-int main() {
+int main(int argc, char **argv) {
     return 0;
 }
`,
			want: true,
		},
		{
			name: "new file from dev null",
			content: `--- /dev/null
+++ newfile.txt
@@ -0,0 +1,2 @@
+hello
+world
`,
			want: true,
		},
		{
			name: "markdown false positive candidate",
			content: `- Item one
- Item two
+ Bonus item
- Item three
`,
			want: false,
		},
		{
			name: "headers without hunk",
			content: `--- a/somefile.txt
+++ b/somefile.txt
Just some content here
No hunk markers at all
`,
			want: false,
		},
		{
			name: "hunk without headers",
			content: `@@ -1,3 +1,4 @@
 some line
-another line
+changed line
`,
			want: false,
		},
		{
			name:    "empty",
			content: "",
			want:    false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()
			if got := IsUnifiedDiff(tt.content); got != tt.want {
				t.Errorf("IsUnifiedDiff() = %v, want %v", got, tt.want)
			}
		})
	}
}