summaryrefslogtreecommitdiff
path: root/env/env_test.go
blob: 59bf40483c2a7554b1e893157caf4e4860f755a6 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package env

import (
	"errors"
	"os"
	"testing"

	"github.com/spf13/afero"

	"github.com/stretchr/testify/assert"
)

func TestGetenv(t *testing.T) {
	assert.Empty(t, Getenv("FOOBARBAZ"))
	assert.Equal(t, os.Getenv("USER"), Getenv("USER"))
	assert.Equal(t, "default value", Getenv("BLAHBLAHBLAH", "default value"))
}

func TestGetenvFile(t *testing.T) {
	fs := afero.NewMemMapFs()
	_ = fs.Mkdir("/tmp", 0777)
	f, _ := fs.Create("/tmp/foo")
	_, _ = f.Write([]byte("foo"))

	defer os.Unsetenv("FOO_FILE")
	os.Setenv("FOO_FILE", "/tmp/foo")
	assert.Equal(t, "foo", getenvVFS(fs, "FOO", "bar"))

	os.Setenv("FOO_FILE", "/tmp/missing")
	assert.Equal(t, "bar", getenvVFS(fs, "FOO", "bar"))

	_, _ = fs.Create("/tmp/unreadable")
	fs = writeOnly(fs)
	os.Setenv("FOO_FILE", "/tmp/unreadable")
	assert.Equal(t, "bar", getenvVFS(fs, "FOO", "bar"))
}

func TestExpandEnv(t *testing.T) {
	assert.Empty(t, ExpandEnv("${FOOBARBAZ}"))
	assert.Equal(t, os.Getenv("USER"), ExpandEnv("$USER"))
	assert.Equal(t, "something", ExpandEnv("something$BLAHBLAHBLAH"))
	assert.Equal(t, os.Getenv("USER")+": "+os.Getenv("HOME"),
		ExpandEnv("$USER: ${HOME}"))
}

func TestExpandEnvFile(t *testing.T) {
	fs := afero.NewMemMapFs()
	_ = fs.Mkdir("/tmp", 0777)
	f, _ := fs.Create("/tmp/foo")
	_, _ = f.Write([]byte("foo"))

	defer os.Unsetenv("FOO_FILE")
	os.Setenv("FOO_FILE", "/tmp/foo")
	assert.Equal(t, "foo is foo", expandEnvVFS(fs, "foo is $FOO"))

	os.Setenv("FOO_FILE", "/tmp/missing")
	assert.Equal(t, "empty", expandEnvVFS(fs, "${FOO}empty"))

	_, _ = fs.Create("/tmp/unreadable")
	fs = writeOnly(fs)
	os.Setenv("FOO_FILE", "/tmp/unreadable")
	assert.Equal(t, "", expandEnvVFS(fs, "${FOO}"))
}

// Maybe extract this into a separate package sometime...
// writeOnly - represents a filesystem that's writeable, but read operations fail
func writeOnly(fs afero.Fs) afero.Fs {
	return &woFS{fs}
}

type woFS struct {
	afero.Fs
}

func (fs woFS) Remove(name string) error {
	return fs.Fs.Remove(name)
}

func (fs woFS) Rename(oldpath, newpath string) error {
	return fs.Fs.Rename(oldpath, newpath)
}

func (fs woFS) Mkdir(name string, perm os.FileMode) error {
	return fs.Fs.Mkdir(name, perm)
}

func (fs woFS) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
	f, err := fs.Fs.OpenFile(name, flag, perm)
	if err != nil {
		return writeOnlyFile(f), err
	}
	return writeOnlyFile(f), nil
}

func (fs woFS) ReadDir(_ string) ([]os.FileInfo, error) {
	return nil, ErrWriteOnly
}

func (fs woFS) Stat(_ string) (os.FileInfo, error) {
	return nil, ErrWriteOnly
}

func writeOnlyFile(f afero.File) afero.File {
	return &woFile{f}
}

type woFile struct {
	afero.File
}

// Write is disabled and returns ErrWriteOnly
func (f woFile) Write(p []byte) (n int, err error) {
	return f.File.Write(p)
}

// Read is disabled and returns ErrWriteOnly
func (f woFile) Read([]byte) (n int, err error) {
	return 0, ErrWriteOnly
}

var ErrWriteOnly = errors.New("filesystem is write-only")