-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmt_test.go
99 lines (77 loc) · 1.63 KB
/
fmt_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"github.com/stretchr/testify/assert"
"io"
"log"
"os"
"path/filepath"
"testing"
)
const accountAlias = "myaccount-123"
var isDryRun = false
var testDir = ""
func mockUi(t *testing.T) Ui {
return Ui{
Logger: log.New(io.Discard, "", 0),
Error: log.New(io.Discard, "", 0),
Debug: log.New(io.Discard, "", 0),
Exit: func(code int) { t.Errorf("ui.Exit called with status %d", code) },
}
}
func TestMain(m *testing.M) {
err := setup()
if err != nil {
log.Fatal(err)
}
code := m.Run()
err = teardown()
if err != nil {
log.Println(err)
}
os.Exit(code)
}
func setup() error {
dryRun = &isDryRun
tempDir, err := os.MkdirTemp("", "iamy-test-fmt")
if err != nil {
return err
}
policyDir := filepath.Join(tempDir, accountAlias, "iam", "policy")
err = os.MkdirAll(policyDir, 0755)
if err != nil {
return err
}
data, err := os.ReadFile("fixtures/resources/iam-policy-before-fmt.yaml")
if err != nil {
return err
}
err = os.WriteFile(filepath.Join(policyDir, "TestPolicy.yaml"), data, 0644)
if err != nil {
return err
}
testDir = tempDir
return nil
}
func teardown() error {
if testDir != "" {
err := os.RemoveAll(testDir)
return err
}
return nil
}
func TestNormalization(t *testing.T) {
expected, err := os.ReadFile("fixtures/resources/iam-policy-after-fmt.yaml")
if err != nil {
t.Fatal(err)
}
input := FormatCommandInput{
Dir: testDir,
CanDelete: true,
}
FormatCommand(mockUi(t), input)
actual, err := os.ReadFile(filepath.Join(testDir, accountAlias, "iam", "policy", "TestPolicy.yaml"))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expected, actual)
}