-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
89 lines (71 loc) · 1.76 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
package main
import (
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
"testing"
)
var fileContent = `
version: '3.2'
services:
base_service:
deploy:
resources:
limits:
cpus: '0.1'
memory: 128M
reservations:
memory: 128M
mongo:
inherit: base_service
container_name: database
image: mongo
`
func InitFile(ext string) error {
err := ioutil.WriteFile(fmt.Sprintf("%s.%s", COMPAT_FILE, ext), []byte(fileContent), 0644)
if err != nil {
return err
}
return nil
}
func RemoveFile(ext string) error {
return os.Remove(fmt.Sprintf("%s.%s", COMPAT_FILE, ext))
}
func TestCompatFileExtensions(t *testing.T) {
extensions := []string{"yml", "yaml"}
for _, ext := range extensions {
if err := InitFile(ext); err != nil {
t.Error("Couldn't create Compat file")
}
cwd, _ := os.Getwd()
t.Run(fmt.Sprintf("should get correct file path for ext: %s", ext), func(it *testing.T) {
path, err := getCompatFilePath(cwd)
if err != nil {
it.Errorf("error getting compat file: %s", err.Error())
}
if !strings.Contains(*path, fmt.Sprintf("%s.%s", COMPAT_FILE, ext)) {
it.Errorf("got the wrong compat file: %s", *path)
}
})
t.Run(fmt.Sprintf("should read the correct content for ext: %s", ext), func(it *testing.T) {
content := readFile(fmt.Sprintf("%s/compat.%s", cwd, ext))
if !reflect.DeepEqual(string(content), fileContent) {
it.Error("content does not match file content")
}
})
if err := RemoveFile(ext); err != nil {
t.Errorf("Couldn't remove comapt.%s", ext)
}
}
}
func TestCompatFileNotFound(t *testing.T) {
cwd, _ := os.Getwd()
t.Run("should return error", func(it *testing.T) {
_, err := getCompatFilePath(cwd)
if err == nil {
it.Errorf("expected error to be returned")
}
})
}