Skip to content

Commit c3d0fa0

Browse files
committed
adds tests for apply output and sample project
1 parent 9cb7d30 commit c3d0fa0

File tree

7 files changed

+65
-43
lines changed

7 files changed

+65
-43
lines changed

cmd/apply_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"testing"
55
)
66

7-
// @TODO mock os.exec and check execution log/transcript?
87
func TestApply(t *testing.T) {
9-
if false {
10-
t.Fatalf("apply failed!")
11-
}
8+
// if false {
9+
// t.Fatalf("apply failed!")
10+
// }
1211
}

internal/config/projectconfig/apply.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// Apply will bootstrap the runtime environment for the project
14-
func Apply(dir string, projectContext *ZeroProjectConfig, applyEnvironments []string) {
14+
func Apply(dir string, projectContext *ZeroProjectConfig, applyEnvironments []string) []string {
1515
flog.Infof(":tada: Bootstrapping project %s. Please use the zero.[hcl, yaml] file to modify the project as needed. %s.", projectContext.Name)
1616

1717
flog.Infof("Cloud provider: %s", "AWS") // will this come from the config?
@@ -22,21 +22,22 @@ func Apply(dir string, projectContext *ZeroProjectConfig, applyEnvironments []st
2222

2323
// other details...
2424

25-
makeAll(dir, projectContext, applyEnvironments)
25+
return makeAll(dir, projectContext, applyEnvironments)
2626
}
2727

28-
func makeAll(dir string, projectContext *ZeroProjectConfig, applyEnvironments []string) error {
28+
func makeAll(dir string, projectContext *ZeroProjectConfig, applyEnvironments []string) []string {
2929
environmentArg := fmt.Sprintf("ENVIRONMENT=%s", strings.Join(applyEnvironments, ","))
3030
envars := []string{environmentArg}
3131

32+
outputs := []string{}
33+
3234
for _, module := range projectContext.Modules {
33-
// TODO what's the root dir for these modules?
34-
// what's the real path to these modules? It's probably not the module name...
35+
// @TODO what's the root dir for these modules?
3536
modulePath := path.Join(dir, projectContext.Name, module.Files.Directory)
36-
37-
// @TODO mock exec?
3837
output := util.ExecuteCommandOutput(exec.Command("make", environmentArg), modulePath, envars)
39-
fmt.Println(output)
38+
flog.Infof("%s", output)
39+
40+
outputs = append(outputs, output)
4041
}
41-
return nil
42+
return outputs
4243
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package projectconfig_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/commitdev/zero/internal/config/projectconfig"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestApply(t *testing.T) {
11+
// @TODO is there a way to do this without relative paths?
12+
// execCMD will use the current folder as target...
13+
dir := "../../../tests/test_data/"
14+
projectName := "sample_project"
15+
projectContext := &projectconfig.ZeroProjectConfig{
16+
Name: projectName,
17+
Modules: projectconfig.EKSGoReactSampleModules(),
18+
}
19+
applyEnvironments := []string{"staging", "production"}
20+
21+
want := []string{
22+
"make module zero-aws-eks-stack\n",
23+
"make module zero-deployable-backend\n",
24+
"make module zero-deployable-react-frontend\n",
25+
}
26+
27+
t.Run("Should run apply and execute make on each folder module", func(t *testing.T) {
28+
got := projectconfig.Apply(dir, projectContext, applyEnvironments)
29+
assert.Equal(t, want, got)
30+
})
31+
}

internal/config/projectconfig/project_config_test.go

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,27 @@ func TestLoadConfig(t *testing.T) {
1818
}
1919
defer os.Remove(file.Name())
2020
file.Write([]byte(validConfigContent()))
21+
filePath := file.Name()
2122

22-
type args struct {
23-
filePath string
24-
}
25-
26-
modules := projectconfig.EKSGoReactSampleModules()
27-
infrastructureModules := projectconfig.InfrastructureSampleModules()
23+
modules := projectconfig.InfrastructureSampleModules()
24+
sampleModules := projectconfig.EKSGoReactSampleModules()
2825

29-
for k, v := range infrastructureModules {
26+
for k, v := range sampleModules {
3027
modules[k] = v
3128
}
3229

33-
expected := &projectconfig.ZeroProjectConfig{
30+
want := &projectconfig.ZeroProjectConfig{
3431
Name: "abc",
3532
Modules: modules,
3633
}
3734

38-
tests := []struct {
39-
name string
40-
args args
41-
want *projectconfig.ZeroProjectConfig
42-
}{
43-
{
44-
"Working config",
45-
args{filePath: file.Name()},
46-
expected,
47-
},
48-
}
49-
for _, tt := range tests {
50-
t.Run(tt.name, func(t *testing.T) {
51-
// @TODO handle nil/empty map unmarshall case?
52-
if got := projectconfig.LoadConfig(tt.args.filePath); !cmp.Equal(got, tt.want, cmpopts.EquateEmpty()) {
53-
t.Errorf(cmp.Diff(got, tt.want))
54-
}
55-
})
56-
}
35+
t.Run("Should load and unmarshall config correctly", func(t *testing.T) {
36+
got := projectconfig.LoadConfig(filePath)
37+
if !cmp.Equal(want, got, cmpopts.EquateEmpty()) {
38+
t.Errorf("projectconfig.ZeroProjectConfig.Unmarshal mismatch (-want +got):\n%s", cmp.Diff(want, got))
39+
}
40+
})
41+
5742
}
5843

5944
func validConfigContent() string {
@@ -72,15 +57,15 @@ modules:
7257
files:
7358
dir: infrastructure
7459
repo: https://github.com/myorg/infrastructure
75-
zero-aws-eks-stack:
60+
aws-eks-stack:
7661
files:
7762
dir: zero-aws-eks-stack
7863
repo: github.com/commitdev/zero-aws-eks-stack
79-
zero-deployable-backend:
64+
deployable-backend:
8065
files:
8166
dir: zero-deployable-backend
8267
repo: github.com/commitdev/zero-deployable-backend
83-
zero-deployable-react-frontend:
68+
deployable-react-frontend:
8469
files:
8570
dir: zero-deployable-react-frontend
8671
repo: github.com/commitdev/zero-deployable-react-frontend
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
current_dir:
2+
@echo "make module" `pwd | xargs basename`
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
current_dir:
2+
@echo "make module" `pwd | xargs basename`
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
current_dir:
2+
@echo "make module" `pwd | xargs basename`

0 commit comments

Comments
 (0)