|
| 1 | +/* |
| 2 | + * Flow CLI |
| 3 | + * |
| 4 | + * Copyright Flow Foundation |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package util |
| 20 | + |
| 21 | +import ( |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +func TestAddFlowEntriesToGitIgnore_NoDuplicates(t *testing.T) { |
| 30 | + _, state, _ := TestMocks(t) |
| 31 | + |
| 32 | + err := AddFlowEntriesToGitIgnore("", state.ReaderWriter()) |
| 33 | + require.NoError(t, err, "Failed to add Flow entries to gitignore") |
| 34 | + |
| 35 | + content, err := state.ReaderWriter().ReadFile(".gitignore") |
| 36 | + require.NoError(t, err, "Failed to read gitignore file") |
| 37 | + |
| 38 | + expectedEntries := []string{"# flow", "emulator-account.pkey", "imports", ".env"} |
| 39 | + for _, entry := range expectedEntries { |
| 40 | + assert.Contains(t, string(content), entry, "Expected gitignore to contain %s", entry) |
| 41 | + } |
| 42 | + |
| 43 | + err = AddFlowEntriesToGitIgnore("", state.ReaderWriter()) |
| 44 | + require.NoError(t, err, "Failed to add Flow entries to gitignore again") |
| 45 | + |
| 46 | + content, err = state.ReaderWriter().ReadFile(".gitignore") |
| 47 | + require.NoError(t, err, "Failed to read gitignore file again") |
| 48 | + |
| 49 | + for _, entry := range expectedEntries { |
| 50 | + occurrences := strings.Count(string(content), entry) |
| 51 | + assert.Equal(t, 1, occurrences, "Expected 1 occurrence of %s, but found %d", entry, occurrences) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestAddFlowEntriesToCursorIgnore_NoDuplicates(t *testing.T) { |
| 56 | + _, state, _ := TestMocks(t) |
| 57 | + |
| 58 | + err := AddFlowEntriesToCursorIgnore("", state.ReaderWriter()) |
| 59 | + require.NoError(t, err, "Failed to add Flow entries to cursorignore") |
| 60 | + |
| 61 | + content, err := state.ReaderWriter().ReadFile(".cursorignore") |
| 62 | + require.NoError(t, err, "Failed to read cursorignore file") |
| 63 | + |
| 64 | + expectedEntries := []string{"# flow", "emulator-account.pkey", ".env", "# Pay attention to imports directory", "!imports/**"} |
| 65 | + for _, entry := range expectedEntries { |
| 66 | + assert.Contains(t, string(content), entry, "Expected cursorignore to contain %s", entry) |
| 67 | + } |
| 68 | + |
| 69 | + err = AddFlowEntriesToCursorIgnore("", state.ReaderWriter()) |
| 70 | + require.NoError(t, err, "Failed to add Flow entries to cursorignore again") |
| 71 | + |
| 72 | + content, err = state.ReaderWriter().ReadFile(".cursorignore") |
| 73 | + require.NoError(t, err, "Failed to read cursorignore file again") |
| 74 | + |
| 75 | + for _, entry := range expectedEntries { |
| 76 | + occurrences := strings.Count(string(content), entry) |
| 77 | + assert.Equal(t, 1, occurrences, "Expected 1 occurrence of %s, but found %d", entry, occurrences) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestAddFlowEntriesToGitIgnore_WithExistingContent(t *testing.T) { |
| 82 | + _, state, _ := TestMocks(t) |
| 83 | + |
| 84 | + existingContent := "# existing content\nnode_modules/\n*.log\n" |
| 85 | + err := state.ReaderWriter().WriteFile(".gitignore", []byte(existingContent), 0644) |
| 86 | + require.NoError(t, err, "Failed to create existing .gitignore") |
| 87 | + |
| 88 | + err = AddFlowEntriesToGitIgnore("", state.ReaderWriter()) |
| 89 | + require.NoError(t, err, "Failed to add Flow entries to gitignore") |
| 90 | + |
| 91 | + content, err := state.ReaderWriter().ReadFile(".gitignore") |
| 92 | + require.NoError(t, err, "Failed to read gitignore file") |
| 93 | + |
| 94 | + assert.Contains(t, string(content), existingContent, "Expected existing content to be preserved") |
| 95 | + |
| 96 | + flowEntries := []string{"# flow", "emulator-account.pkey", "imports", ".env"} |
| 97 | + for _, entry := range flowEntries { |
| 98 | + assert.Contains(t, string(content), entry, "Expected gitignore to contain %s", entry) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestAddFlowEntriesToCursorIgnore_WithExistingContent(t *testing.T) { |
| 103 | + _, state, _ := TestMocks(t) |
| 104 | + |
| 105 | + existingContent := "# existing cursor ignore\n.vscode/\n.idea/\n" |
| 106 | + err := state.ReaderWriter().WriteFile(".cursorignore", []byte(existingContent), 0644) |
| 107 | + require.NoError(t, err, "Failed to create existing .cursorignore") |
| 108 | + |
| 109 | + err = AddFlowEntriesToCursorIgnore("", state.ReaderWriter()) |
| 110 | + require.NoError(t, err, "Failed to add Flow entries to cursorignore") |
| 111 | + |
| 112 | + content, err := state.ReaderWriter().ReadFile(".cursorignore") |
| 113 | + require.NoError(t, err, "Failed to read cursorignore file") |
| 114 | + |
| 115 | + assert.Contains(t, string(content), existingContent, "Expected existing content to be preserved") |
| 116 | + |
| 117 | + flowEntries := []string{"# flow", "emulator-account.pkey", ".env", "# Pay attention to imports directory", "!imports/**"} |
| 118 | + for _, entry := range flowEntries { |
| 119 | + assert.Contains(t, string(content), entry, "Expected cursorignore to contain %s", entry) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestAddEntriesToIgnoreFile_HelperFunction(t *testing.T) { |
| 124 | + _, state, _ := TestMocks(t) |
| 125 | + |
| 126 | + entries := []string{"# test", "test-file.txt", "another-file.log"} |
| 127 | + err := addEntriesToIgnoreFile("test-ignore.txt", entries, state.ReaderWriter()) |
| 128 | + require.NoError(t, err, "Failed to add entries to ignore file") |
| 129 | + |
| 130 | + content, err := state.ReaderWriter().ReadFile("test-ignore.txt") |
| 131 | + require.NoError(t, err, "Failed to read ignore file") |
| 132 | + |
| 133 | + for _, entry := range entries { |
| 134 | + assert.Contains(t, string(content), entry, "Expected ignore file to contain %s", entry) |
| 135 | + } |
| 136 | + |
| 137 | + err = addEntriesToIgnoreFile("test-ignore.txt", entries, state.ReaderWriter()) |
| 138 | + require.NoError(t, err, "Failed to add entries to ignore file again") |
| 139 | + |
| 140 | + content, err = state.ReaderWriter().ReadFile("test-ignore.txt") |
| 141 | + require.NoError(t, err, "Failed to read ignore file again") |
| 142 | + |
| 143 | + for _, entry := range entries { |
| 144 | + occurrences := strings.Count(string(content), entry) |
| 145 | + assert.Equal(t, 1, occurrences, "Expected 1 occurrence of %s, but found %d", entry, occurrences) |
| 146 | + } |
| 147 | +} |
0 commit comments