|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "github.com/studyzy/codei18n/core/config" |
| 13 | + "github.com/studyzy/codei18n/core/workflow" |
| 14 | +) |
| 15 | + |
| 16 | +func TestMapUpdate_RespectsExcludePatterns(t *testing.T) { |
| 17 | + // Setup temporary directory |
| 18 | + tempDir, err := os.MkdirTemp("", "codei18n_test_exclude") |
| 19 | + if err != nil { |
| 20 | + t.Fatal(err) |
| 21 | + } |
| 22 | + defer os.RemoveAll(tempDir) |
| 23 | + |
| 24 | + // Create a valid source file |
| 25 | + srcDir := filepath.Join(tempDir, "src") |
| 26 | + os.Mkdir(srcDir, 0755) |
| 27 | + srcFile := filepath.Join(srcDir, "main.go") |
| 28 | + os.WriteFile(srcFile, []byte(`package main |
| 29 | +// Valid comment |
| 30 | +func main() {}`), 0644) |
| 31 | + |
| 32 | + // Create a node_modules directory which should be excluded |
| 33 | + nodeModulesDir := filepath.Join(tempDir, "node_modules", "lib") |
| 34 | + os.MkdirAll(nodeModulesDir, 0755) |
| 35 | + ignoredFile := filepath.Join(nodeModulesDir, "ignored.ts") |
| 36 | + os.WriteFile(ignoredFile, []byte(` |
| 37 | +// Ignored comment |
| 38 | +const x = 1;`), 0644) |
| 39 | + |
| 40 | + // Create config with excludePatterns |
| 41 | + cfg := &config.Config{ |
| 42 | + SourceLanguage: "en", |
| 43 | + LocalLanguage: "zh-CN", |
| 44 | + ExcludePatterns: []string{ |
| 45 | + "node_modules/**", |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + // Run Map Update Workflow |
| 50 | + // Use MapUpdate function |
| 51 | + |
| 52 | + // Create .codei18n dir in CURRENT WORKDIR (because MapUpdate uses hardcoded path relative to CWD mostly, or we should change CWD) |
| 53 | + // MapUpdate uses filepath.Join(".codei18n", "mappings.json") which is relative. |
| 54 | + // So we need to switch CWD to tempDir for this test to work properly without modifying MapUpdate signature yet. |
| 55 | + cwd, _ := os.Getwd() |
| 56 | + defer os.Chdir(cwd) |
| 57 | + os.Chdir(tempDir) |
| 58 | + |
| 59 | + storePath := filepath.Join(".codei18n", "mappings.json") |
| 60 | + os.MkdirAll(filepath.Dir(storePath), 0755) |
| 61 | + |
| 62 | + res, err := workflow.MapUpdate(cfg, tempDir, false) |
| 63 | + assert.NoError(t, err) |
| 64 | + assert.NotNil(t, res) |
| 65 | + |
| 66 | + // Check the store content |
| 67 | + content, err := os.ReadFile(storePath) |
| 68 | + assert.NoError(t, err) |
| 69 | + |
| 70 | + // We expect "Valid comment" to be present |
| 71 | + assert.Contains(t, string(content), "Valid comment") |
| 72 | + |
| 73 | + // We expect "Ignored comment" to be ABSENT |
| 74 | + // If the bug exists, this will likely fail (it will contain "Ignored comment") |
| 75 | + if assert.NotContains(t, string(content), "Ignored comment", "Should not contain comments from node_modules") { |
| 76 | + t.Log("Exclude patterns worked correctly!") |
| 77 | + } else { |
| 78 | + t.Log("Exclude patterns FAILED: found ignored comment") |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestConvert_RespectsExcludePatterns(t *testing.T) { |
| 83 | + if testing.Short() { |
| 84 | + t.Skip("Skipping integration test in short mode") |
| 85 | + } |
| 86 | + bin := GetBinaryPath(t) |
| 87 | + |
| 88 | + // Setup temporary directory |
| 89 | + tempDir := t.TempDir() |
| 90 | + |
| 91 | + // Create a node_modules directory which should be excluded |
| 92 | + nodeModulesDir := filepath.Join(tempDir, "node_modules", "lib") |
| 93 | + os.MkdirAll(nodeModulesDir, 0755) |
| 94 | + ignoredFile := filepath.Join(nodeModulesDir, "ignored.ts") |
| 95 | + os.WriteFile(ignoredFile, []byte(` |
| 96 | +// Ignored comment |
| 97 | +const x = 1;`), 0644) |
| 98 | + |
| 99 | + // Create a valid source file |
| 100 | + srcDir := filepath.Join(tempDir, "src") |
| 101 | + os.MkdirAll(srcDir, 0755) |
| 102 | + validFile := filepath.Join(srcDir, "valid.ts") |
| 103 | + os.WriteFile(validFile, []byte(` |
| 104 | +// Valid comment |
| 105 | +const y = 2;`), 0644) |
| 106 | + |
| 107 | + // Create config with excludePatterns |
| 108 | + cfg := &config.Config{ |
| 109 | + SourceLanguage: "en", |
| 110 | + LocalLanguage: "zh-CN", |
| 111 | + ExcludePatterns: []string{ |
| 112 | + "node_modules/**", |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + // Create .codei18n directory and write config |
| 117 | + codei18nDir := filepath.Join(tempDir, ".codei18n") |
| 118 | + os.MkdirAll(codei18nDir, 0755) |
| 119 | + cfgBytes, _ := json.Marshal(cfg) |
| 120 | + os.WriteFile(filepath.Join(codei18nDir, "config.json"), cfgBytes, 0644) |
| 121 | + |
| 122 | + // Run convert --dry-run |
| 123 | + cmd := exec.Command(bin, "convert", "--to", "zh-CN", "--dry-run", "--dir", ".", "--verbose") |
| 124 | + cmd.Dir = tempDir |
| 125 | + |
| 126 | + out, err := cmd.CombinedOutput() |
| 127 | + require.NoError(t, err, "Convert command failed: %s", string(out)) |
| 128 | + output := string(out) |
| 129 | + |
| 130 | + // Since we haven't created mappings, convert won't do much, but it scans files. |
| 131 | + // In verbose mode, or if it errors on finding files, we might see output. |
| 132 | + // But relying on logs is brittle. |
| 133 | + |
| 134 | + // Let's create mappings manually so convert has something to do |
| 135 | + // We need IDs. |
| 136 | + // Valid comment ID: SHA1(...) |
| 137 | + // Ignored comment ID: SHA1(...) |
| 138 | + |
| 139 | + // But easier way: Check if ignored file is even looked at. |
| 140 | + // The log "Scanning directory failed" would appear if bad. |
| 141 | + // Or just trust my previous unit test for map update which uses same exclude logic? |
| 142 | + // MapUpdate uses scanner.Directory. Convert uses manual walk. |
| 143 | + // So I MUST test Convert logic. |
| 144 | + |
| 145 | + // If I put "Ignored comment" in mapping, convert should NOT try to replace it if file is excluded. |
| 146 | + // If file is excluded, it's not in 'files' list. processFile is not called. |
| 147 | + // So no "Converting..." log. |
| 148 | + |
| 149 | + // Let's rely on Dry Run output. |
| 150 | + // If files are processed, log.Info("准备处理 %d 个文件...", len(files)) is printed. |
| 151 | + // Since verbose is on (flag passed to binary? --verbose is global flag). |
| 152 | + // I passed --verbose. |
| 153 | + |
| 154 | + // Check for "准备处理 1 个文件..." (should be 1, not 2). |
| 155 | + if assert.Contains(t, output, "准备处理 1 个文件") { |
| 156 | + t.Log("Convert correctly identified 1 file") |
| 157 | + } else { |
| 158 | + t.Logf("Convert output did not match expectation: %s", output) |
| 159 | + } |
| 160 | + |
| 161 | + assert.NotContains(t, output, "ignored.ts") |
| 162 | +} |
0 commit comments