Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undeterministic components order and refactor sorting logic #14

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions internal/gitlab/component_documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io/fs"
"slices"
"text/template"

"github.com/erNail/labdoc/internal/yamlutils"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
138 changes: 138 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 @@ -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)
}
51 changes: 0 additions & 51 deletions internal/gitlab/gitlab_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -107,8 +104,6 @@ func parseSpecInputs(specNode yaml.Node) []Input {
inputs = append(inputs, input)
}

inputs = sortSpecInputs(inputs)

return inputs
}

Expand Down Expand Up @@ -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
}
56 changes: 3 additions & 53 deletions internal/gitlab/gitlab_component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 := `---
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Loading