Skip to content

Commit 667a96a

Browse files
committed
feat: add validation for platform
1 parent 55b8f1f commit 667a96a

4 files changed

Lines changed: 121 additions & 7 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package validator
2+
3+
import (
4+
"errors"
5+
"slices"
6+
7+
"github.com/aslamcodes/powerappstream-builder/internal/config"
8+
)
9+
10+
var (
11+
ErrPlatformMissing = errors.New("config file does not contain platform specification")
12+
ErrInvalidPlatform = errors.New("Platform not supported")
13+
)
14+
15+
16+
func ValidateConfig(c *config.Config) error {
17+
if c.Platform == "" {
18+
return ErrPlatformMissing
19+
}
20+
21+
supportedPlatforms := make([]string, len(ExecPlatformMap))
22+
23+
for k := range ExecPlatformMap {
24+
supportedPlatforms = append(supportedPlatforms, k)
25+
}
26+
27+
if !slices.Contains(supportedPlatforms, c.Platform) {
28+
return ErrInvalidPlatform
29+
}
30+
31+
return nil
32+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package validator_test
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"testing"
8+
9+
"github.com/aslamcodes/powerappstream-builder/internal/backend"
10+
"github.com/aslamcodes/powerappstream-builder/internal/validator"
11+
)
12+
13+
func TestValidateConfig(t *testing.T) {
14+
testCases := []struct {
15+
desc string
16+
fileContent string
17+
expected error
18+
}{
19+
{
20+
desc: "valid config unix",
21+
fileContent: `platform: "unix"
22+
installers:
23+
- executable: "bash"
24+
installScript: |
25+
echo "Hello World"`,
26+
expected: nil,
27+
},
28+
{
29+
desc: "valid config windows",
30+
fileContent: `platform: "windows"
31+
installers:
32+
- executable: "bash"
33+
installScript: |
34+
echo "Hello World"`,
35+
expected: nil,
36+
},
37+
{
38+
desc: "invalid config",
39+
fileContent: `platform: "non_existent"
40+
installers:
41+
- executable: "bash"
42+
installScript: |
43+
echo "Hello World"`,
44+
expected: validator.ErrInvalidPlatform,
45+
},
46+
}
47+
for _, tC := range testCases {
48+
t.Run(tC.desc, func(t *testing.T) {
49+
file, err := os.CreateTemp("../../testdata", fmt.Sprintf("test_%s_", tC.desc))
50+
51+
if err != nil {
52+
t.Errorf("unable to create temp file: %v", err)
53+
}
54+
55+
file.WriteString(tC.fileContent)
56+
57+
lb := backend.LocalBackend{
58+
Location: file.Name(),
59+
}
60+
61+
configData, err := lb.GetConfig()
62+
63+
if err != nil {
64+
t.Errorf("unable to fetch config data: %v", err)
65+
}
66+
67+
err = validator.ValidateConfig(configData)
68+
69+
if !errors.Is(err, tC.expected) {
70+
t.Errorf("expected %v, got %v", tC.expected, err)
71+
}
72+
73+
t.Cleanup(func() {
74+
os.Remove(file.Name())
75+
})
76+
77+
t.Cleanup(func() {
78+
file.Close()
79+
})
80+
})
81+
}
82+
}

internal/validator/installer_validator.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,12 @@ import (
1010

1111
var (
1212
ErrInvalidExecutableForPlatform = errors.New("Invalid executable for given platform")
13-
ErrInvalidPlatform = errors.New("Platform not supported")
1413
)
1514

1615
func InstallerValidator(configData *config.Config) error {
17-
execPlatformMap := map[string][]string{
18-
"windows": {"powershell"},
19-
"unix": {"bash"},
20-
}
21-
2216
platform := configData.Platform
2317

24-
platformExecs, exists := execPlatformMap[platform]
18+
platformExecs, exists := ExecPlatformMap[platform]
2519

2620
if !exists {
2721
return ErrInvalidPlatform

internal/validator/validator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package validator
2+
3+
var ExecPlatformMap = map[string][]string{
4+
"windows": {"powershell"},
5+
"unix": {"bash"},
6+
}

0 commit comments

Comments
 (0)