diff --git a/internal/gitlab/component_documentation.go b/internal/gitlab/component_documentation.go index f368596..f4dcb19 100644 --- a/internal/gitlab/component_documentation.go +++ b/internal/gitlab/component_documentation.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io/fs" + "slices" "text/template" "github.com/erNail/labdoc/internal/yamlutils" @@ -99,6 +100,12 @@ func buildComponentDocumentationFromComponents( repoURL string, version string, ) ComponentsDocumentation { + components = sortComponents(components) + for _, component := range components { + component.Inputs = sortInputs(component.Inputs) + component.Jobs = sortJobs(component.Jobs) + } + componentDocumentation := ComponentsDocumentation{ Components: components, RepoURL: repoURL, @@ -219,3 +226,72 @@ func compareExistingDocumentation(filesystem afero.Fs, outputFilePath string, ne return nil } + +// sortJobs sorts a slice of Job structs by their name. +// +// Parameters: +// - jobs: A slice of Job structs. +// +// Returns: +// - []Job: The sorted slice of Job structs. +func sortJobs(jobs []Job) []Job { + slices.SortFunc(jobs, func(a Job, b Job) int { + if a.Name < b.Name { + return -1 + } + + if a.Name > b.Name { + return 1 + } + + return 0 + }) + + return jobs +} + +// sortInputs sorts a slice of Input structs by their name. +// +// Parameters: +// - inputs: A slice of Input structs. +// +// Returns: +// - []Input: The sorted slice of Input structs. +func sortInputs(inputs []Input) []Input { + slices.SortFunc(inputs, func(a Input, b Input) int { + if a.Name < b.Name { + return -1 + } + + if a.Name > b.Name { + return 1 + } + + return 0 + }) + + 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 +} diff --git a/internal/gitlab/component_documentation_test.go b/internal/gitlab/component_documentation_test.go index 6be6eb2..069ef3e 100644 --- a/internal/gitlab/component_documentation_test.go +++ b/internal/gitlab/component_documentation_test.go @@ -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() @@ -267,3 +339,69 @@ Description: Description2 actualContent := renderDocumentationContent(componentsDocumentation, templateFilePath, filesystem) assert.Equal(t, expectedContent, actualContent) } + +func TestSortJobsCorrectlySortsJobs(t *testing.T) { + t.Parallel() + + jobA := Job{ + Name: "A", + } + jobB := Job{ + Name: "B", + } + jobs := []Job{ + jobB, + jobA, + } + + expectedJobs := []Job{ + jobA, + jobB, + } + + sortJobs(jobs) + assert.Equal(t, expectedJobs, jobs) +} + +func TestSortSpecInputsCorrectlySortsInputs(t *testing.T) { + t.Parallel() + + inputA := Input{ + Name: "A", + } + inputB := Input{ + Name: "B", + } + inputs := []Input{ + inputB, + inputA, + } + + expectedInputs := []Input{ + inputA, + inputB, + } + + 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) +} diff --git a/internal/gitlab/gitlab_component.go b/internal/gitlab/gitlab_component.go index 8f2cbdb..5b40bb4 100644 --- a/internal/gitlab/gitlab_component.go +++ b/internal/gitlab/gitlab_component.go @@ -70,12 +70,9 @@ func (gitlabCiConfig *CiConfig) UnmarshalYAML(node *yaml.Node) error { Comment: yamlutils.FormatCommentAsPlainText(keyNode.HeadComment), } gitlabCiConfig.Jobs = append(gitlabCiConfig.Jobs, job) - gitlabCiConfig.Jobs = sortJobs(gitlabCiConfig.Jobs) } } - gitlabCiConfig.Jobs = sortJobs(gitlabCiConfig.Jobs) - return nil } @@ -107,8 +104,6 @@ func parseSpecInputs(specNode yaml.Node) []Input { inputs = append(inputs, input) } - inputs = sortSpecInputs(inputs) - return inputs } @@ -198,49 +193,3 @@ func generateComponentNameFromFilePath(filePath string) string { return filename } - -// sortJobs sorts a slice of Job structs by their name. -// -// Parameters: -// - jobs: A slice of Job structs. -// -// Returns: -// - []Job: The sorted slice of Job structs. -func sortJobs(jobs []Job) []Job { - slices.SortFunc(jobs, func(a Job, b Job) int { - if a.Name < b.Name { - return -1 - } - - if a.Name > b.Name { - return 1 - } - - return 0 - }) - - return jobs -} - -// sortSpecInputs sorts a slice of Input structs by their name. -// -// Parameters: -// - inputs: A slice of Input structs. -// -// Returns: -// - []Input: The sorted slice of Input structs. -func sortSpecInputs(inputs []Input) []Input { - slices.SortFunc(inputs, func(a Input, b Input) int { - if a.Name < b.Name { - return -1 - } - - if a.Name > b.Name { - return 1 - } - - return 0 - }) - - return inputs -} diff --git a/internal/gitlab/gitlab_component_test.go b/internal/gitlab/gitlab_component_test.go index dd7606a..79d8996 100644 --- a/internal/gitlab/gitlab_component_test.go +++ b/internal/gitlab/gitlab_component_test.go @@ -3,7 +3,6 @@ package gitlab import ( "testing" - log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" @@ -33,7 +32,7 @@ job: {} assert.Equal(t, "string-input", gitlabCiConfig.Spec.Inputs[0].Name) } -func TestParseYamlWithoutSeparatorsCreatesGitLabCiConfigWithSortedArrays(t *testing.T) { +func TestParseYamlWithoutSeparatorsCreatesGitLabCiConfig(t *testing.T) { t.Parallel() yamlFileContent := `--- @@ -115,12 +114,9 @@ second-job: {} }, } - expectedGitLabCiConfig.Jobs = sortJobs(expectedGitLabCiConfig.Jobs) - expectedGitLabCiConfig.Spec.Inputs = sortSpecInputs(expectedGitLabCiConfig.Spec.Inputs) - actualGitLabCiConfig := parseYamlFileWithoutSeparatorsToGitLabCiConfig([]byte(yamlFileContent)) - log.Info(actualGitLabCiConfig) - assert.Equal(t, expectedGitLabCiConfig, actualGitLabCiConfig) + assert.ElementsMatch(t, expectedGitLabCiConfig.Jobs, actualGitLabCiConfig.Jobs) + assert.ElementsMatch(t, expectedGitLabCiConfig.Spec.Inputs, actualGitLabCiConfig.Spec.Inputs) } func TestNewComponentFromGitLabCiConfigCreatesValidGitLabCiConfig(t *testing.T) { @@ -213,49 +209,3 @@ func TestGenerateComponentNameFromFilePathResultsInCorrectName(t *testing.T) { actualName = generateComponentNameFromFilePath("file.yml") assert.Equal(t, expectedName, actualName) } - -func TestSortJobsCorrectlySortsJobs(t *testing.T) { - t.Parallel() - - jobA := Job{ - Name: "A", - } - jobB := Job{ - Name: "B", - } - jobs := []Job{ - jobB, - jobA, - } - - expectedJobs := []Job{ - jobA, - jobB, - } - - sortJobs(jobs) - assert.Equal(t, expectedJobs, jobs) -} - -func TestSortSpecInputsCorrectlySortsInputs(t *testing.T) { - t.Parallel() - - inputA := Input{ - Name: "A", - } - inputB := Input{ - Name: "B", - } - inputs := []Input{ - inputB, - inputA, - } - - expectedInputs := []Input{ - inputA, - inputB, - } - - sortSpecInputs(inputs) - assert.Equal(t, expectedInputs, inputs) -}