summaryrefslogtreecommitdiff
path: root/internal/datafs/getenv_test.go
blob: 8e5cc4dfeb7a54b0de153cc111989336f69b8eb8 (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
package datafs

import (
	"errors"
	"io/fs"
	"testing"
	"testing/fstest"

	"github.com/hack-pad/hackpadfs"

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

func TestGetenvFsys(t *testing.T) {
	fsys := fs.FS(fstest.MapFS{
		"tmp":            &fstest.MapFile{Mode: fs.ModeDir | 0o777},
		"tmp/foo":        &fstest.MapFile{Data: []byte("foo")},
		"tmp/unreadable": &fstest.MapFile{Data: []byte("foo"), Mode: 0o000},
	})
	fsys = WrapWdFS(fsys)

	t.Setenv("FOO_FILE", "/tmp/foo")
	assert.Equal(t, "foo", GetenvFsys(fsys, "FOO", "bar"))

	t.Setenv("FOO_FILE", "/tmp/missing")
	assert.Equal(t, "bar", GetenvFsys(fsys, "FOO", "bar"))

	fsys = writeOnly(fsys)
	t.Setenv("FOO_FILE", "/tmp/unreadable")
	assert.Equal(t, "bar", GetenvFsys(fsys, "FOO", "bar"))
}

func TestExpandEnvFsys(t *testing.T) {
	fsys := fs.FS(fstest.MapFS{
		"tmp":            &fstest.MapFile{Mode: fs.ModeDir | 0o777},
		"tmp/foo":        &fstest.MapFile{Data: []byte("foo")},
		"tmp/unreadable": &fstest.MapFile{Data: []byte("foo"), Mode: 0o000},
	})
	fsys = WrapWdFS(fsys)

	t.Setenv("FOO_FILE", "/tmp/foo")
	assert.Equal(t, "foo is foo", ExpandEnvFsys(fsys, "foo is $FOO"))

	t.Setenv("FOO_FILE", "/tmp/missing")
	assert.Equal(t, "empty", ExpandEnvFsys(fsys, "${FOO}empty"))

	fsys = writeOnly(fsys)
	t.Setenv("FOO_FILE", "/tmp/unreadable")
	assert.Empty(t, ExpandEnvFsys(fsys, "${FOO}"))
}

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

type woFS struct {
	fsys fs.FS
}

func (fsys woFS) Open(name string) (fs.File, error) {
	f, err := fsys.fsys.Open(name)
	return writeOnlyFile(f), err
}

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

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

func writeOnlyFile(f fs.File) fs.File {
	if f == nil {
		return nil
	}

	return &woFile{f}
}

type woFile struct {
	fs.File
}

// Write -
func (f woFile) Write(p []byte) (n int, err error) {
	return hackpadfs.WriteFile(f.File, 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")