-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
94 lines (82 loc) · 2.45 KB
/
main_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
package main
import (
"os"
"strings"
"testing"
"github.com/oferchen/hclalign/cli"
"github.com/oferchen/hclalign/config"
"github.com/spf13/cobra"
)
// Helper function to create a temporary HCL file.
func createTempHCLFile(t *testing.T, content string) string {
t.Helper()
tempFile, err := os.CreateTemp("", "*.hcl")
if err != nil {
t.Fatalf("Failed to create temp HCL file: %v", err)
}
defer tempFile.Close()
if _, err := tempFile.WriteString(content); err != nil {
t.Fatalf("Failed to write to temp HCL file: %v", err)
}
return tempFile.Name()
}
func TestMainFunctionality(t *testing.T) {
tests := []struct {
name string
setup func(*testing.T) []string
wantErr bool
errMsg string
}{
{
name: "Missing Target Argument",
setup: func(t *testing.T) []string {
return []string{} // No args means the target is missing
},
wantErr: true,
errMsg: "accepts 1 arg(s), received 0",
},
{
name: "Valid Single File",
setup: func(t *testing.T) []string {
// Create a temp file with valid HCL content
filePath := createTempHCLFile(t, `variable "test" {}`)
return []string{filePath}
},
wantErr: false,
},
{
name: "Multiple Files",
setup: func(t *testing.T) []string {
// Create two temp files, but since the command only accepts one, this should cause an error
filePath1 := createTempHCLFile(t, `variable "test1" {}`)
filePath2 := createTempHCLFile(t, `variable "test2" {}`)
return []string{filePath1, filePath2}
},
wantErr: true,
errMsg: "accepts 1 arg(s), received 2",
},
}
// Iterate over test cases
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Setup
args := tc.setup(t)
rootCmd := &cobra.Command{
Use: "hcl_align [target file or directory]",
Short: "Aligns HCL files based on given criteria",
Args: cobra.ExactArgs(1),
RunE: cli.RunE,
}
rootCmd.Flags().StringSliceP("criteria", "c", config.DefaultCriteria, "List of file criteria to align")
rootCmd.Flags().StringSliceP("order", "o", config.DefaultOrder, "Comma-separated list of the order of variable block fields")
rootCmd.SetArgs(args)
_, err := rootCmd.ExecuteC()
if (err != nil) != tc.wantErr {
t.Fatalf("Unexpected error status: got error = %v, wantErr = %v", err, tc.wantErr)
}
if tc.wantErr && !strings.Contains(err.Error(), tc.errMsg) {
t.Errorf("Expected error message to contain '%s', but got '%s'", tc.errMsg, err.Error())
}
})
}
}