-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
69 lines (59 loc) · 1.56 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
package main
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"reflect"
"testing"
)
func TestMain(t *testing.T) {
// Build the Go application
cmd := exec.Command("go", "build")
err := cmd.Run()
if err != nil {
t.Fatalf("Failed to build application: %v", err)
}
// Move the built file to the test directory
err = os.Rename("purge", "test/purge")
if err != nil {
t.Fatalf("Failed to move built file: %v", err)
}
// Get the working directory,
// Combine the directory, and
// Run the built file
dir, _ := os.Getwd()
purgePath := filepath.Join(dir, "test", "purge")
cmd = exec.Command(purgePath)
cmd.Stderr = os.Stderr
cmd.Dir = filepath.Dir(purgePath)
err = cmd.Run()
if err != nil {
t.Fatalf("Failed to run built file: %v", err)
}
// Read the package.json file and expected.json file
// And Compare them
packageJSON, err := os.ReadFile("test/package.json")
if err != nil {
t.Fatalf("Failed to read package.json: %v", err)
}
expectedJSON, err := os.ReadFile("test/expected.json")
if err != nil {
t.Fatalf("Failed to read expected.json: %v", err)
}
// Unmarshal the JSON data
var packageData map[string]interface{}
var expectedData map[string]interface{}
err = json.Unmarshal(packageJSON, &packageData)
if err != nil {
t.Fatalf("Failed to unmarshal package.json: %v", err)
}
err = json.Unmarshal(expectedJSON, &expectedData)
if err != nil {
t.Fatalf("Failed to unmarshal expected.json: %v", err)
}
// Compare the data
if !reflect.DeepEqual(packageData, expectedData) {
t.Fatalf("package.json does not match expected.json")
}
}