Skip to content

Commit

Permalink
fix: Make the order of documented components deterministic by sorting…
Browse files Browse the repository at this point in the history
… the components by name
  • Loading branch information
erNail committed Jun 11, 2024
1 parent 8075326 commit 714ab1d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/gitlab/component_documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func buildComponentDocumentationFromComponents(
repoURL string,
version string,
) ComponentsDocumentation {
components = sortComponents(components)
for _, component := range components {
component.Inputs = sortInputs(component.Inputs)
component.Jobs = sortJobs(component.Jobs)
Expand Down Expand Up @@ -271,3 +272,26 @@ func sortInputs(inputs []Input) []Input {

return inputs
}

// sortComponents sorts a slice of Component structs by their name.
//
// Parameters:
// - components: A slice of Component structs.
//
// Returns:
// - []Component: The sorted slice of Component structs.
func sortComponents(components []Component) []Component {
slices.SortFunc(components, func(a Component, b Component) int {
if a.Name < b.Name {
return -1
}

if a.Name > b.Name {
return 1
}

return 0
})

return components
}
92 changes: 92 additions & 0 deletions internal/gitlab/component_documentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,78 @@ second-component-second-job: {}
assert.Contains(t, string(outputContent), "This is component first-component")
}

func TestBuildComponentDocumentationFromComponentsBuildsSortedComponentDocumentation(t *testing.T) {
t.Parallel()

components := []Component{
{
Name: "ComponentB",
Description: "Second component",
Inputs: []Input{
{Name: "InputB", Description: "Second input"},
{Name: "InputA", Description: "First input"},
},
Jobs: []Job{
{Name: "JobB", Comment: "Second job"},
{Name: "JobA", Comment: "First job"},
},
},
{
Name: "ComponentA",
Description: "First component",
Inputs: []Input{
{Name: "InputB", Description: "Second input"},
{Name: "InputA", Description: "First input"},
},
Jobs: []Job{
{Name: "JobB", Comment: "Second job"},
{Name: "JobA", Comment: "First job"},
},
},
}

repoURL := "https://example.com/repo"
version := "1.0.0"

// Expected sorted components, inputs, and jobs
expectedComponents := []Component{
{
Name: "ComponentA",
Description: "First component",
Inputs: []Input{
{Name: "InputA", Description: "First input"},
{Name: "InputB", Description: "Second input"},
},
Jobs: []Job{
{Name: "JobA", Comment: "First job"},
{Name: "JobB", Comment: "Second job"},
},
},
{
Name: "ComponentB",
Description: "Second component",
Inputs: []Input{
{Name: "InputA", Description: "First input"},
{Name: "InputB", Description: "Second input"},
},
Jobs: []Job{
{Name: "JobA", Comment: "First job"},
{Name: "JobB", Comment: "Second job"},
},
},
}

expected := ComponentsDocumentation{
Components: expectedComponents,
RepoURL: repoURL,
Version: version,
}

result := buildComponentDocumentationFromComponents(components, repoURL, version)

assert.Equal(t, expected, result)
}

func TestReadTemplateFileReadsEmbeddedFile(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -313,3 +385,23 @@ func TestSortSpecInputsCorrectlySortsInputs(t *testing.T) {
sortInputs(inputs)
assert.Equal(t, expectedInputs, inputs)
}

func TestSortComponentsCorrectlySortsComponents(t *testing.T) {
t.Parallel()

components := []Component{
{Name: "beta"},
{Name: "alpha"},
{Name: "gamma"},
}

expectedComponents := []Component{
{Name: "alpha"},
{Name: "beta"},
{Name: "gamma"},
}

actualComponents := sortComponents(components)

assert.Equal(t, expectedComponents, actualComponents)
}

0 comments on commit 714ab1d

Please sign in to comment.