Skip to content

Commit e7a26b4

Browse files
authored
Added test framework, and init command tests (#20)
* Added end-to-end test framework * Added various unit tests * Remove windows platform from build
1 parent 57cbad4 commit e7a26b4

24 files changed

+2077
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/peridot

.goreleaser.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ builds:
1010
- CGO_ENABLED=0
1111
goos:
1212
- linux
13-
- windows
1413
- darwin
1514
goarch:
1615
- "386"

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
default: test
22

3+
.PHONY: test
34
test:
45
go test -race ./...
56

_examples/basic/custom/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ variables:
77
default: 37
88
files:
99
- target: {{ .user_home_dir }}{{ .sep }}example.txt
10-
template: ./custom.tpl
10+
template: ./custom.tmpl

go.mod

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,38 @@ module github.com/liamg/peridot
33
go 1.17
44

55
require (
6+
github.com/docker/docker v20.10.12+incompatible
67
github.com/hexops/gotextdiff v1.0.3
78
github.com/liamg/tml v0.4.0
89
github.com/spf13/cobra v1.3.0
910
github.com/stretchr/testify v1.7.0
1011
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
12+
gotest.tools v2.2.0+incompatible
1113
)
1214

1315
require (
16+
github.com/Microsoft/go-winio v0.4.17 // indirect
17+
github.com/containerd/containerd v1.5.8 // indirect
1418
github.com/davecgh/go-spew v1.1.1 // indirect
19+
github.com/docker/distribution v2.7.1+incompatible // indirect
20+
github.com/docker/go-connections v0.4.0 // indirect
21+
github.com/docker/go-units v0.4.0 // indirect
22+
github.com/gogo/protobuf v1.3.2 // indirect
23+
github.com/golang/protobuf v1.5.2 // indirect
24+
github.com/google/go-cmp v0.5.6 // indirect
25+
github.com/gorilla/mux v1.8.0 // indirect
1526
github.com/inconshreveable/mousetrap v1.0.0 // indirect
27+
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
28+
github.com/morikuni/aec v1.0.0 // indirect
29+
github.com/opencontainers/go-digest v1.0.0 // indirect
30+
github.com/opencontainers/image-spec v1.0.2 // indirect
31+
github.com/pkg/errors v0.9.1 // indirect
1632
github.com/pmezard/go-difflib v1.0.0 // indirect
33+
github.com/sirupsen/logrus v1.8.1 // indirect
1734
github.com/spf13/pflag v1.0.5 // indirect
35+
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
36+
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
37+
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
38+
google.golang.org/grpc v1.42.0 // indirect
39+
google.golang.org/protobuf v1.27.1 // indirect
1840
)

go.sum

Lines changed: 556 additions & 1 deletion
Large diffs are not rendered by default.

internal/pkg/builtins/git.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ var (
5252
[commit]
5353
gpgsign = true
5454
[core]
55+
excludesfile = ~/.gitignore
5556
{{ if .editor }} editor = {{ .editor }}
5657
{{ else }} editor = vim
5758
{{ end }}
58-
[ignore]
59-
file = ~/.gitignore
6059
[pull]
6160
rebase = true
6261
[alias]

internal/pkg/cmd/init.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"os"
6+
"path/filepath"
57

68
"github.com/liamg/peridot/internal/pkg/config"
7-
"github.com/liamg/peridot/internal/pkg/module"
89
"github.com/liamg/tml"
910
"github.com/spf13/cobra"
1011
)
@@ -15,10 +16,13 @@ func init() {
1516
Use: "init",
1617
Short: "Initialises a new peridot config for the local user environment.",
1718
Run: func(cmd *cobra.Command, args []string) {
18-
root, err := module.LoadRoot()
19-
if err == nil {
19+
configPath, err := config.Path()
20+
if err != nil {
21+
fail(fmt.Sprintf("Cannot locate config path: %s", err))
22+
}
23+
if _, err := os.Stat(configPath); err == nil {
2024
if force {
21-
if err := os.RemoveAll(root.Path()); err != nil {
25+
if err := os.RemoveAll(filepath.Dir(configPath)); err != nil {
2226
fail(err)
2327
}
2428
} else {

internal/pkg/cmd/root.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ import (
1212
var rootCmd = &cobra.Command{
1313
Use: "peridot",
1414
Short: "Manage dotfiles and user environments across machines, OSes, users and more.",
15+
PersistentPreRun: func(_ *cobra.Command, args []string) {
16+
if disableANSI {
17+
tml.DisableFormatting()
18+
}
19+
},
20+
}
21+
22+
var disableANSI bool
23+
24+
func init() {
25+
rootCmd.PersistentFlags().BoolVar(&disableANSI, "no-ansi", disableANSI, "Disable ANSI colour/formatting codes in output.")
1526
}
1627

1728
func Execute() {

internal/pkg/config/module_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestModule_Validate(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
module Module
13+
wantErr bool
14+
}{
15+
{
16+
name: "empty config",
17+
module: Module{},
18+
wantErr: false,
19+
},
20+
{
21+
name: "variable with no name",
22+
module: Module{
23+
Variables: []Variable{
24+
{
25+
Name: "",
26+
},
27+
},
28+
},
29+
wantErr: true,
30+
},
31+
{
32+
name: "variable not required but no default",
33+
module: Module{
34+
Variables: []Variable{
35+
{
36+
Name: "x",
37+
Required: false,
38+
Default: nil,
39+
},
40+
},
41+
},
42+
wantErr: true,
43+
},
44+
{
45+
name: "variable required but has default",
46+
module: Module{
47+
Variables: []Variable{
48+
{
49+
Name: "x",
50+
Required: true,
51+
Default: "blah",
52+
},
53+
},
54+
},
55+
wantErr: true,
56+
},
57+
{
58+
name: "child module without name",
59+
module: Module{
60+
Modules: []InnerModule{
61+
{
62+
Name: "",
63+
},
64+
},
65+
},
66+
wantErr: true,
67+
},
68+
{
69+
name: "child module without source",
70+
module: Module{
71+
Modules: []InnerModule{
72+
{
73+
Name: "X",
74+
Source: "",
75+
},
76+
},
77+
},
78+
wantErr: true,
79+
},
80+
{
81+
name: "scripts: has install_required but no install",
82+
module: Module{
83+
Scripts: Scripts{
84+
InstallRequired: "check",
85+
Install: "",
86+
},
87+
},
88+
wantErr: true,
89+
},
90+
{
91+
name: "scripts: has no install_required but has install",
92+
module: Module{
93+
Scripts: Scripts{
94+
InstallRequired: "",
95+
Install: "check",
96+
},
97+
},
98+
wantErr: true,
99+
},
100+
{
101+
name: "scripts: has update_required but no update",
102+
module: Module{
103+
Scripts: Scripts{
104+
UpdateRequired: "check",
105+
Update: "",
106+
},
107+
},
108+
wantErr: true,
109+
},
110+
{
111+
name: "scripts: has no update_required but has update",
112+
module: Module{
113+
Scripts: Scripts{
114+
UpdateRequired: "",
115+
Update: "check",
116+
},
117+
},
118+
wantErr: true,
119+
},
120+
{
121+
name: "scripts: has after_file_change but no files",
122+
module: Module{
123+
Scripts: Scripts{
124+
AfterFileChange: "check",
125+
},
126+
},
127+
wantErr: true,
128+
},
129+
}
130+
for _, tt := range tests {
131+
t.Run(tt.name, func(t *testing.T) {
132+
err := tt.module.Validate()
133+
if tt.wantErr {
134+
assert.Error(t, err)
135+
} else {
136+
assert.NoError(t, err)
137+
}
138+
})
139+
}
140+
}

internal/pkg/system/system_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package system
2+
3+
import (
4+
"runtime"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestInfo(t *testing.T) {
11+
info := Info()
12+
assert.Equal(t, runtime.GOOS, info.OperatingSystem)
13+
assert.Equal(t, runtime.GOARCH, info.Architecture)
14+
}

internal/pkg/variable/collection.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ type collection struct {
2222
}
2323

2424
func (c *collection) Has(name string) bool {
25-
return c.Get(name) != nil
25+
_, ok := c.data[name]
26+
return ok
2627
}
2728

2829
func (c *collection) Get(name string) Variable {

0 commit comments

Comments
 (0)