-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathutil_test.go
More file actions
147 lines (111 loc) · 5.56 KB
/
util_test.go
File metadata and controls
147 lines (111 loc) · 5.56 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
/*
* Flow CLI
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package util
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAddFlowEntriesToGitIgnore_NoDuplicates(t *testing.T) {
_, state, _ := TestMocks(t)
err := AddFlowEntriesToGitIgnore("", state.ReaderWriter())
require.NoError(t, err, "Failed to add Flow entries to gitignore")
content, err := state.ReaderWriter().ReadFile(".gitignore")
require.NoError(t, err, "Failed to read gitignore file")
expectedEntries := []string{"# flow", "emulator-account.pkey", "imports", ".env"}
for _, entry := range expectedEntries {
assert.Contains(t, string(content), entry, "Expected gitignore to contain %s", entry)
}
err = AddFlowEntriesToGitIgnore("", state.ReaderWriter())
require.NoError(t, err, "Failed to add Flow entries to gitignore again")
content, err = state.ReaderWriter().ReadFile(".gitignore")
require.NoError(t, err, "Failed to read gitignore file again")
for _, entry := range expectedEntries {
occurrences := strings.Count(string(content), entry)
assert.Equal(t, 1, occurrences, "Expected 1 occurrence of %s, but found %d", entry, occurrences)
}
}
func TestAddFlowEntriesToCursorIgnore_NoDuplicates(t *testing.T) {
_, state, _ := TestMocks(t)
err := AddFlowEntriesToCursorIgnore("", state.ReaderWriter())
require.NoError(t, err, "Failed to add Flow entries to cursorignore")
content, err := state.ReaderWriter().ReadFile(".cursorignore")
require.NoError(t, err, "Failed to read cursorignore file")
expectedEntries := []string{"# flow", "emulator-account.pkey", ".env", "# Pay attention to imports directory", "!imports/**"}
for _, entry := range expectedEntries {
assert.Contains(t, string(content), entry, "Expected cursorignore to contain %s", entry)
}
err = AddFlowEntriesToCursorIgnore("", state.ReaderWriter())
require.NoError(t, err, "Failed to add Flow entries to cursorignore again")
content, err = state.ReaderWriter().ReadFile(".cursorignore")
require.NoError(t, err, "Failed to read cursorignore file again")
for _, entry := range expectedEntries {
occurrences := strings.Count(string(content), entry)
assert.Equal(t, 1, occurrences, "Expected 1 occurrence of %s, but found %d", entry, occurrences)
}
}
func TestAddFlowEntriesToGitIgnore_WithExistingContent(t *testing.T) {
_, state, _ := TestMocks(t)
existingContent := "# existing content\nnode_modules/\n*.log\n"
err := state.ReaderWriter().WriteFile(".gitignore", []byte(existingContent), 0644)
require.NoError(t, err, "Failed to create existing .gitignore")
err = AddFlowEntriesToGitIgnore("", state.ReaderWriter())
require.NoError(t, err, "Failed to add Flow entries to gitignore")
content, err := state.ReaderWriter().ReadFile(".gitignore")
require.NoError(t, err, "Failed to read gitignore file")
assert.Contains(t, string(content), existingContent, "Expected existing content to be preserved")
flowEntries := []string{"# flow", "emulator-account.pkey", "imports", ".env"}
for _, entry := range flowEntries {
assert.Contains(t, string(content), entry, "Expected gitignore to contain %s", entry)
}
}
func TestAddFlowEntriesToCursorIgnore_WithExistingContent(t *testing.T) {
_, state, _ := TestMocks(t)
existingContent := "# existing cursor ignore\n.vscode/\n.idea/\n"
err := state.ReaderWriter().WriteFile(".cursorignore", []byte(existingContent), 0644)
require.NoError(t, err, "Failed to create existing .cursorignore")
err = AddFlowEntriesToCursorIgnore("", state.ReaderWriter())
require.NoError(t, err, "Failed to add Flow entries to cursorignore")
content, err := state.ReaderWriter().ReadFile(".cursorignore")
require.NoError(t, err, "Failed to read cursorignore file")
assert.Contains(t, string(content), existingContent, "Expected existing content to be preserved")
flowEntries := []string{"# flow", "emulator-account.pkey", ".env", "# Pay attention to imports directory", "!imports/**"}
for _, entry := range flowEntries {
assert.Contains(t, string(content), entry, "Expected cursorignore to contain %s", entry)
}
}
func TestAddEntriesToIgnoreFile_HelperFunction(t *testing.T) {
_, state, _ := TestMocks(t)
entries := []string{"# test", "test-file.txt", "another-file.log"}
err := addEntriesToIgnoreFile("test-ignore.txt", entries, state.ReaderWriter())
require.NoError(t, err, "Failed to add entries to ignore file")
content, err := state.ReaderWriter().ReadFile("test-ignore.txt")
require.NoError(t, err, "Failed to read ignore file")
for _, entry := range entries {
assert.Contains(t, string(content), entry, "Expected ignore file to contain %s", entry)
}
err = addEntriesToIgnoreFile("test-ignore.txt", entries, state.ReaderWriter())
require.NoError(t, err, "Failed to add entries to ignore file again")
content, err = state.ReaderWriter().ReadFile("test-ignore.txt")
require.NoError(t, err, "Failed to read ignore file again")
for _, entry := range entries {
occurrences := strings.Count(string(content), entry)
assert.Equal(t, 1, occurrences, "Expected 1 occurrence of %s, but found %d", entry, occurrences)
}
}