-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
61 lines (46 loc) · 1.48 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
package main
import (
"fmt"
"os"
"strings"
"testing"
"testing/iotest"
"github.com/stretchr/testify/assert"
)
func TestRun(t *testing.T) {
input := "ENVIRONMENT1=hello\nENVIRONMENT2=world\nENVIRONMENT3=\n"
reader := strings.NewReader(input)
output, err := run(reader, "docker-compose.yaml")
assert.NoError(t, err)
b, err := os.ReadFile("docker-compose.yaml.expected")
assert.NoError(t, err)
assert.Equal(t, string(b), output)
}
func TestRunNoFile(t *testing.T) {
reader := strings.NewReader("")
_, err := run(reader, "nofile")
assert.Error(t, err)
assert.ErrorContains(t, err, "open nofile: no such file or directory")
}
func TestRunErrorStdin(t *testing.T) {
reader := iotest.ErrReader(fmt.Errorf("error reading string from stdin"))
_, err := run(reader, "docker-compose.yaml.expected")
assert.Error(t, err)
assert.ErrorContains(t, err, "error reading string from stdin")
}
func TestReadEnvVars(t *testing.T) {
input := "ENVIRONMENT1=hello\nENVIRONMENT2=world\nENVIRONMENT3=\n\n"
reader := strings.NewReader(input)
envVars, err := readEnvVars(reader)
assert.NoError(t, err)
assert.Equal(t, 2, len(envVars))
assert.Equal(t, "ENVIRONMENT1=hello", envVars[0])
assert.Equal(t, "ENVIRONMENT2=world", envVars[1])
}
func TestReadEnvVarsErrors(t *testing.T) {
reader := iotest.ErrReader(fmt.Errorf("error reading string from stdin"))
envVars, err := readEnvVars(reader)
assert.Error(t, err)
assert.ErrorContains(t, err, "error reading string from stdin")
assert.Nil(t, envVars)
}