-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
212 lines (173 loc) · 5.85 KB
/
main_test.go
File metadata and controls
212 lines (173 loc) · 5.85 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFetchGitignoreSuccess(t *testing.T) {
// Create a test server that returns a sample .gitignore
expectedContent := "# Python\n*.pyc\n__pycache__/\n"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(expectedContent))
}))
defer server.Close()
// Test that the source URL format is correct
expectedURL := "https://raw.githubusercontent.com/github/gitignore/main/Python.gitignore"
actualURL := fmt.Sprintf(source, "Python")
assert.Equal(t, expectedURL, actualURL)
}
func TestSourceURLFormat(t *testing.T) {
tests := []struct {
lang string
expected string
}{
{"Python", "https://raw.githubusercontent.com/github/gitignore/main/Python.gitignore"},
{"Go", "https://raw.githubusercontent.com/github/gitignore/main/Go.gitignore"},
{"Node", "https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore"},
{"Java", "https://raw.githubusercontent.com/github/gitignore/main/Java.gitignore"},
}
for _, tt := range tests {
t.Run(tt.lang, func(t *testing.T) {
actual := fmt.Sprintf(source, tt.lang)
assert.Equal(t, tt.expected, actual)
})
}
}
func TestTargetFilename(t *testing.T) {
assert.Equal(t, ".gitignore", target)
}
func TestHTTPGetSuccess(t *testing.T) {
expectedContent := "# Test content\n*.log\n"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(expectedContent))
}))
defer server.Close()
resp, err := http.Get(server.URL)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, expectedContent, string(body))
}
func TestHTTPGet404(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 page not found"))
}))
defer server.Close()
resp, err := http.Get(server.URL)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
}
func TestFileAppendOperation(t *testing.T) {
tempDir := t.TempDir()
testFile := tempDir + "/.gitignore"
// Write initial content
initialContent := "# Initial content\n"
err := os.WriteFile(testFile, []byte(initialContent), 0644)
require.NoError(t, err)
// Append new content (simulating what the tool does)
file, err := os.OpenFile(testFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
require.NoError(t, err)
defer file.Close()
newContent := "# Appended content\n*.pyc\n"
_, err = file.WriteString(newContent)
require.NoError(t, err)
// Read and verify
finalContent, err := os.ReadFile(testFile)
require.NoError(t, err)
expected := initialContent + newContent
assert.Equal(t, expected, string(finalContent))
}
func TestFileCreationIfNotExists(t *testing.T) {
tempDir := t.TempDir()
testFile := tempDir + "/.gitignore"
// File shouldn't exist yet
_, err := os.Stat(testFile)
assert.True(t, os.IsNotExist(err))
// Create file with O_CREATE flag
file, err := os.OpenFile(testFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
require.NoError(t, err)
defer file.Close()
content := "# New file\n*.log\n"
_, err = file.WriteString(content)
require.NoError(t, err)
// Verify file was created
finalContent, err := os.ReadFile(testFile)
require.NoError(t, err)
assert.Equal(t, content, string(finalContent))
}
func TestIOCopy(t *testing.T) {
content := "# Test content\nnode_modules/\n*.log\n"
reader := strings.NewReader(content)
tempDir := t.TempDir()
testFile := tempDir + "/.gitignore"
file, err := os.Create(testFile)
require.NoError(t, err)
defer file.Close()
written, err := io.Copy(file, reader)
require.NoError(t, err)
assert.Equal(t, int64(len(content)), written)
// Verify content
finalContent, err := os.ReadFile(testFile)
require.NoError(t, err)
assert.Equal(t, content, string(finalContent))
}
func TestMultipleLanguageFormats(t *testing.T) {
languages := []string{"Python", "Go", "Node", "Java", "Rust", "Ruby"}
for _, lang := range languages {
t.Run(lang, func(t *testing.T) {
url := fmt.Sprintf(source, lang)
assert.Contains(t, url, lang)
assert.Contains(t, url, ".gitignore")
assert.Contains(t, url, "raw.githubusercontent.com")
})
}
}
func TestFilePermissions(t *testing.T) {
tempDir := t.TempDir()
testFile := tempDir + "/.gitignore"
file, err := os.OpenFile(testFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
require.NoError(t, err)
file.Close()
info, err := os.Stat(testFile)
require.NoError(t, err)
// Check that file has correct permissions (0644)
// On some systems, this might be modified by umask
mode := info.Mode().Perm()
assert.True(t, mode&0400 != 0, "Owner should have read permission")
assert.True(t, mode&0200 != 0, "Owner should have write permission")
}
func TestEmptyLanguageHandling(t *testing.T) {
// This tests the URL format when lang is empty
// In the actual tool, this is caught by the flag check
url := fmt.Sprintf(source, "")
expected := "https://raw.githubusercontent.com/github/gitignore/main/.gitignore"
assert.Equal(t, expected, url)
}
func TestSpecialCharactersInLanguage(t *testing.T) {
tests := []struct {
lang string
expected string
}{
{"C++", "https://raw.githubusercontent.com/github/gitignore/main/C++.gitignore"},
{"Objective-C", "https://raw.githubusercontent.com/github/gitignore/main/Objective-C.gitignore"},
{"VisualStudio", "https://raw.githubusercontent.com/github/gitignore/main/VisualStudio.gitignore"},
}
for _, tt := range tests {
t.Run(tt.lang, func(t *testing.T) {
actual := fmt.Sprintf(source, tt.lang)
assert.Equal(t, tt.expected, actual)
})
}
}