Skip to content

Commit e549467

Browse files
committed
fix(xml): Support tree-only mode in XML format
Add FilePaths field to TemplateData to provide file paths independently of file contents. This allows XML format to build the directory structure correctly in tree-only mode when Files array is empty. - Add FilePaths to TemplateData struct - Populate FilePaths in PrepareTemplateData (always, not just tree-only) - Update XML format to use FilePaths for directory structure - Update tests to include and verify FilePaths field
1 parent 7899043 commit e549467

5 files changed

Lines changed: 32 additions & 4 deletions

File tree

internal/generator/format.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type FileData struct {
1414
type TemplateData struct {
1515
Structure string
1616
Files []FileData
17+
FilePaths []string
1718
}
1819

1920
// Format defines the interface for different output formats

internal/generator/formats/formats_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,15 @@ func createTestTemplateData() generator.TemplateData {
153153
Language: "go",
154154
},
155155
},
156+
FilePaths: []string{"main.go"},
156157
}
157158
}
158159

159160
func createTreeOnlyTemplateData() generator.TemplateData {
160161
return generator.TemplateData{
161162
Structure: "test-project/\n├── src/\n│ └── main.go\n└── README.md\n",
162163
Files: []generator.FileData{},
164+
FilePaths: []string{"src/main.go", "README.md"},
163165
}
164166
}
165167

@@ -224,8 +226,19 @@ func TestXMLFormatTreeOnly(t *testing.T) {
224226
if !strings.Contains(content, "<project>") {
225227
t.Errorf("Expected content to contain '<project>'")
226228
}
229+
// Directory structure should be built from FilePaths
230+
if !strings.Contains(content, "<directory name=\"src\">") {
231+
t.Errorf("Expected content to contain '<directory name=\"src\">' from FilePaths")
232+
}
233+
if !strings.Contains(content, "<file name=\"main.go\">") {
234+
t.Errorf("Expected content to contain '<file name=\"main.go\">' in filesystem section")
235+
}
236+
if !strings.Contains(content, "<file name=\"README.md\">") {
237+
t.Errorf("Expected content to contain '<file name=\"README.md\">' in filesystem section")
238+
}
239+
// Files section should be empty (no file contents)
227240
if strings.Contains(content, "<file path=") {
228-
t.Errorf("Expected content to NOT contain file elements in tree-only mode")
241+
t.Errorf("Expected content to NOT contain file content elements in tree-only mode")
229242
}
230243
if tokens <= 0 {
231244
t.Errorf("Expected tokens to be positive, got %d", tokens)

internal/generator/formats/xml.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ func (f *XMLFormat) Render(data generator.TemplateData) (string, int, error) {
6161
files: []string{},
6262
}
6363

64-
// Get all file paths from the files data
65-
for _, file := range data.Files {
66-
addFileToTree(root, file.Path)
64+
for _, path := range data.FilePaths {
65+
addFileToTree(root, path)
6766
}
6867

6968
// Convert our internal tree to the XML structure

internal/generator/generator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ func (g *Generator) PrepareTemplateData() (TemplateData, error) {
202202

203203
g.SelectedFiles = expandedSelection
204204

205+
filePaths := make([]string, 0, len(expandedSelection))
206+
for path := range expandedSelection {
207+
filePaths = append(filePaths, path)
208+
}
209+
205210
rootNode := g.buildTree()
206211
var structureBuilder strings.Builder
207212
baseRootName := filepath.Base(g.RootPath)
@@ -239,5 +244,6 @@ func (g *Generator) PrepareTemplateData() (TemplateData, error) {
239244
return TemplateData{
240245
Structure: structureBuilder.String(),
241246
Files: filesData,
247+
FilePaths: filePaths,
242248
}, nil
243249
}

internal/generator/generator_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ func TestPrepareTemplateData(t *testing.T) {
151151
t.Errorf("Expected language for %q to be %q, got %q", tf.path, "go", file.Language)
152152
}
153153
}
154+
155+
// Verify FilePaths is also populated
156+
if len(data.FilePaths) != 3 {
157+
t.Errorf("Expected FilePaths to have 3 entries, got %d", len(data.FilePaths))
158+
}
154159
}
155160

156161
func TestGenerateString(t *testing.T) {
@@ -282,6 +287,10 @@ func TestPrepareTemplateDataTreeOnly(t *testing.T) {
282287
if len(data.Files) != 0 {
283288
t.Errorf("Expected Files to be empty in tree-only mode, got %d files", len(data.Files))
284289
}
290+
// FilePaths should still be populated for XML format support
291+
if len(data.FilePaths) != 3 {
292+
t.Errorf("Expected FilePaths to have 3 entries in tree-only mode, got %d", len(data.FilePaths))
293+
}
285294
}
286295

287296
type mockFormat struct {

0 commit comments

Comments
 (0)