Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aknysh committed Mar 9, 2025
1 parent 815a0b3 commit 0cf449a
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/about.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cmd
import (
_ "embed"

"github.com/cloudposse/atmos/pkg/utils"
"github.com/spf13/cobra"

"github.com/cloudposse/atmos/pkg/utils"
)

//go:embed markdown/about.md
Expand Down
35 changes: 35 additions & 0 deletions cmd/about_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"bytes"
"io"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestAboutCmd(t *testing.T) {
// Capture stdout since utils.PrintfMarkdown writes directly to os.Stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

// Execute the command
err := aboutCmd.RunE(aboutCmd, []string{})
assert.NoError(t, err, "'atmos about' command should execute without error")

// Close the writer and restore stdout
err = w.Close()
assert.NoError(t, err, "'atmos about' command should execute without error")

os.Stdout = oldStdout

// Read captured output
var output bytes.Buffer
_, err = io.Copy(&output, r)
assert.NoError(t, err, "'atmos about' command should execute without error")

// Check if output contains expected markdown content
assert.Contains(t, output.String(), aboutMarkdown, "'atmos about' output should contain information about Atmos")
}
3 changes: 2 additions & 1 deletion cmd/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cmd
import (
_ "embed"

"github.com/cloudposse/atmos/pkg/utils"
"github.com/spf13/cobra"

"github.com/cloudposse/atmos/pkg/utils"
)

//go:embed markdown/support.md
Expand Down
35 changes: 35 additions & 0 deletions cmd/support_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"bytes"
"io"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSupportCmd(t *testing.T) {
// Capture stdout since utils.PrintfMarkdown writes directly to os.Stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

// Execute the command
err := supportCmd.RunE(aboutCmd, []string{})
assert.NoError(t, err, "'atmos support' command should execute without error")

// Close the writer and restore stdout
err = w.Close()
assert.NoError(t, err, "'atmos support' command should execute without error")

os.Stdout = oldStdout

// Read captured output
var output bytes.Buffer
_, err = io.Copy(&output, r)
assert.NoError(t, err, "'atmos support' command should execute without error")

// Check if output contains expected markdown content
assert.Contains(t, output.String(), supportMarkdown, "'atmos support' output should contain information about Cloud Posse Atmos support")
}
3 changes: 1 addition & 2 deletions internal/exec/stack_processor_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,7 @@ func ProcessYAMLConfigFile(
nil
}

// ProcessStackConfig takes a stack manifest, deep-merges all variables, settings, environments and backends,
// and returns the final stack configuration for all Terraform and helmfile components
// ProcessStackConfig takes a stack manifest, deep-merges all variables, settings, environments and backends, and returns the final stack configuration for all Terraform and helmfile components.
func ProcessStackConfig(

Check failure on line 551 in internal/exec/stack_processor_utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] internal/exec/stack_processor_utils.go#L551

cognitive complexity 729 of func `ProcessStackConfig` is high (> 20) (gocognit)
Raw output
internal/exec/stack_processor_utils.go:551:1: cognitive complexity 729 of func `ProcessStackConfig` is high (> 20) (gocognit)
func ProcessStackConfig(
^
atmosConfig schema.AtmosConfiguration,

Check failure on line 552 in internal/exec/stack_processor_utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] internal/exec/stack_processor_utils.go#L552

hugeParam: atmosConfig is heavy (5760 bytes); consider passing it by pointer (gocritic)
Raw output
internal/exec/stack_processor_utils.go:552:2: hugeParam: atmosConfig is heavy (5760 bytes); consider passing it by pointer (gocritic)
	atmosConfig schema.AtmosConfiguration,
	^
stacksBasePath string,
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/vendor_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func ExecuteVendorPullCommand(cmd *cobra.Command, args []string) error {
"Example: atmos vendor pull -c <component>\n%s", q)

Check warning on line 137 in internal/exec/vendor_utils.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/vendor_utils.go#L136-L137

Added lines #L136 - L137 were not covered by tests
}

// ReadAndProcessVendorConfigFile reads and processes the Atmos vendoring config file `vendor.yaml`
// ReadAndProcessVendorConfigFile reads and processes the Atmos vendoring config file `vendor.yaml`.
func ReadAndProcessVendorConfigFile(

Check failure on line 141 in internal/exec/vendor_utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] internal/exec/vendor_utils.go#L141

cognitive complexity 41 of func `ReadAndProcessVendorConfigFile` is high (> 20) (gocognit)
Raw output
internal/exec/vendor_utils.go:141:1: cognitive complexity 41 of func `ReadAndProcessVendorConfigFile` is high (> 20) (gocognit)
func ReadAndProcessVendorConfigFile(
^
atmosConfig schema.AtmosConfiguration,

Check failure on line 142 in internal/exec/vendor_utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] internal/exec/vendor_utils.go#L142

hugeParam: atmosConfig is heavy (5760 bytes); consider passing it by pointer (gocritic)
Raw output
internal/exec/vendor_utils.go:142:2: hugeParam: atmosConfig is heavy (5760 bytes); consider passing it by pointer (gocritic)
	atmosConfig schema.AtmosConfiguration,
	^
vendorConfigFile string,
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func ExecuteWorkflowCmd(cmd *cobra.Command, args []string) error {
validWorkflows = append(validWorkflows, fmt.Sprintf("\n- %s", w))
}
// sorting so that the output is deterministic
sort.Sort(sort.StringSlice(validWorkflows))
sort.Strings(validWorkflows)

Check warning on line 126 in internal/exec/workflow.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/workflow.go#L126

Added line #L126 was not covered by tests
errorMarkdown += strings.Join(validWorkflows, "")
return fmt.Errorf("%s", errorMarkdown)

Check failure on line 128 in internal/exec/workflow.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] internal/exec/workflow.go#L128

do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"%s\", errorMarkdown)" (err113)
Raw output
internal/exec/workflow.go:128:10: do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"%s\", errorMarkdown)" (err113)
		return fmt.Errorf("%s", errorMarkdown)
		       ^

Check warning on line 128 in internal/exec/workflow.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/workflow.go#L128

Added line #L128 was not covered by tests
} else {
Expand Down
48 changes: 48 additions & 0 deletions pkg/stack/stack_processor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package stack

import (
"sort"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -245,3 +246,50 @@ func TestStackProcessorRelativePaths(t *testing.T) {
myappComponent := terraformComponents["myapp"].(map[string]any)
assert.NotNil(t, myappComponent)
}

func TestProcessYAMLConfigFile(t *testing.T) {
stacksBasePath := "../../tests/fixtures/scenarios/relative-paths/stacks"
filePath := "../../tests/fixtures/scenarios/relative-paths/stacks/orgs/acme/platform/dev.yaml"

atmosConfig := schema.AtmosConfiguration{
Templates: schema.Templates{
Settings: schema.TemplatesSettings{
Enabled: true,
Sprig: schema.TemplatesSettingsSprig{
Enabled: true,
},
Gomplate: schema.TemplatesSettingsGomplate{
Enabled: true,
},
},
},
}

_, _, stackConfigMap, _, _, _, _, err := ProcessYAMLConfigFile(
atmosConfig,
stacksBasePath,
filePath,
map[string]map[string]any{},
nil,
false,
false,
true,
false,
nil,
nil,
nil,
nil,
"",
)

assert.Nil(t, err)
assert.Equal(t, 3, len(stackConfigMap))

mapResultKeys := u.StringKeysFromMap(stackConfigMap)
// sorting so that the output is deterministic
sort.Strings(mapResultKeys)

assert.Equal(t, "components", mapResultKeys[0])
assert.Equal(t, "import", mapResultKeys[1])
assert.Equal(t, "vars", mapResultKeys[2])
}
6 changes: 3 additions & 3 deletions pkg/utils/markdown_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"os/exec"
"runtime/debug"

"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/ui/markdown"
l "github.com/charmbracelet/log"

Check failure on line 12 in pkg/utils/markdown_utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/utils/markdown_utils.go#L12

import "github.com/charmbracelet/log" imported as "l" but must be "log" according to config (importas)
Raw output
pkg/utils/markdown_utils.go:12:2: import "github.com/charmbracelet/log" imported as "l" but must be "log" according to config (importas)
	l "github.com/charmbracelet/log"
	^
"golang.org/x/text/cases"
"golang.org/x/text/language"

l "github.com/charmbracelet/log"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/ui/markdown"
)

// render is the global markdown renderer instance initialized via InitializeMarkdown

Check failure on line 20 in pkg/utils/markdown_utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/utils/markdown_utils.go#L20

Comment should end in a period (godot)
Raw output
pkg/utils/markdown_utils.go:20:1: Comment should end in a period (godot)
// render is the global markdown renderer instance initialized via InitializeMarkdown
^
Expand Down

0 comments on commit 0cf449a

Please sign in to comment.