-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtestenv.go
More file actions
187 lines (145 loc) · 5.59 KB
/
testenv.go
File metadata and controls
187 lines (145 loc) · 5.59 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package testutils
import (
"context"
"log/slog"
"os/exec"
"path/filepath"
"testing"
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev"
"github.com/microsoft/azure-linux-dev-tools/internal/global/opctx"
"github.com/microsoft/azure-linux-dev-tools/internal/global/testctx"
"github.com/microsoft/azure-linux-dev-tools/internal/projectconfig"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"github.com/thejerf/slogassert"
)
// Test environment, useful for unit-testing azldev CLI commands. Contains
// an [azldev.Env] constructed with injected dependencies that redirect
// filesystem and OS environmental access to included test objects.
type TestEnv struct {
Env *azldev.Env
Config *projectconfig.ProjectConfig
TestInterfaces azldev.SystemInterfaces
// Test implementations
CmdFactory *testctx.TestCmdFactory
DryRunnable opctx.DryRunnable
EventListener opctx.EventListener
TestFS opctx.FS
TestOSEnv opctx.OSEnv
CommandsExecuted [][]string
}
// Ensure that [TestEnv.Env] implements [opctx.Ctx].
var _ opctx.Ctx = TestEnv{}.Env
// Ensure that [TestEnv.Env] implements [context.Context].
var _ context.Context = TestEnv{}.Env
// Constructs a new [TestEnv].
func NewTestEnv(t *testing.T) *TestEnv {
t.Helper()
const (
testProjectDir = "/project"
testMockConfigPath = testProjectDir + "/mock.cfg"
)
testEnv := newTestEnv(testMockConfigPath)
populateTestProjectFiles(t, testEnv, testProjectDir, testMockConfigPath)
setCmdFactory(testEnv)
setEnvDependencies(t, testEnv)
setEnv(t, testEnv, testProjectDir)
return testEnv
}
// newTestEnv creates a new [TestEnv] with a test project config
// and mock implementations of [opctx.FS] and [opctx.OSEnv].
func newTestEnv(testMockConfigPath string) *TestEnv {
return &TestEnv{
Config: constructProjectConfig(testMockConfigPath),
CommandsExecuted: [][]string{},
TestFS: afero.NewMemMapFs(),
TestOSEnv: testctx.NewTestOSEnv(),
}
}
// setEnvDependencies sets interface dependencies shared by [azldev.Env].
func setEnvDependencies(t *testing.T, testEnv *TestEnv) {
t.Helper()
setUpEventListener(t, testEnv)
testEnv.TestInterfaces = azldev.SystemInterfaces{
CmdFactory: testEnv.CmdFactory,
FileSystemFactory: testEnv,
OSEnvFactory: testEnv,
}
testEnv.DryRunnable = azldev.NewAppDryRunnable(false)
}
func setUpEventListener(t *testing.T, testEnv *TestEnv) {
t.Helper()
testLogHandler := slogassert.New(t, slog.LevelDebug, nil)
testEventLogger := slog.New(testLogHandler)
testEventListener, err := azldev.NewEventListener(testEventLogger, false)
require.NoError(t, err)
testEnv.EventListener = testEventListener
}
// populateTestProjectFiles creates simple mock config and project files
// in the test project directory.
func populateTestProjectFiles(t *testing.T, testEnv *TestEnv, testProjectDir string, testMockConfigPath string) {
t.Helper()
// Create the project dir.
require.NoError(t, testEnv.TestOSEnv.Chdir(testProjectDir))
// Create an empty mock config file.
require.NoError(t, fileutils.WriteFile(testEnv.TestFS, testMockConfigPath, []byte{}, fileperms.PrivateFile))
// Create an empty project (.toml) config file.
_, err := testEnv.TestFS.Create(filepath.Join(testProjectDir, projectconfig.DefaultConfigFileName))
require.NoError(t, err)
}
// setEnv sets the [azldev.Env] for the test environment, using the provided
// project directory and the test environment's configuration.
func setEnv(t *testing.T, testEnv *TestEnv, testProjectDir string) {
t.Helper()
envOptions := azldev.NewEnvOptions()
envOptions.DryRunnable = testEnv.DryRunnable
envOptions.EventListener = testEnv.EventListener
envOptions.Interfaces = testEnv.TestInterfaces
envOptions.ProjectDir = testProjectDir
envOptions.Config = testEnv.Config
testEnv.Env = azldev.NewEnv(t.Context(), envOptions)
}
// setCmdFactory sets the test version of [testctx.CmdFactory] for the test environment.
func setCmdFactory(testEnv *TestEnv) {
testEnv.CmdFactory = testctx.NewTestCmdFactory()
testEnv.CmdFactory.RunHandler = func(cmd *exec.Cmd) error {
testEnv.CommandsExecuted = append(testEnv.CommandsExecuted, cmd.Args)
return nil
}
testEnv.CmdFactory.RunAndGetOutputHandler = func(cmd *exec.Cmd) (string, error) {
testEnv.CommandsExecuted = append(testEnv.CommandsExecuted, cmd.Args)
return "", nil
}
}
// Construct a [projectconfig.ProjectConfig].
func constructProjectConfig(testMockConfigPath string) *projectconfig.ProjectConfig {
config := projectconfig.NewProjectConfig()
config.Project.WorkDir = "/work"
config.Project.LogDir = "/logs"
config.Project.OutputDir = "/output"
config.Project.DefaultDistro.Name = "test-distro"
config.Project.DefaultDistro.Version = "1.0"
distro := projectconfig.DistroDefinition{
Versions: make(map[string]projectconfig.DistroVersionDefinition),
LookasideBaseURI: "https://example.com/lookaside/$pkg/$filename/$hashtype/$hash/$filename",
DistGitBaseURI: "https://example.com/upstream/$pkg.git",
}
distro.Versions["1.0"] = projectconfig.DistroVersionDefinition{
MockConfigPath: testMockConfigPath,
DistGitBranch: "main",
}
config.Distros["test-distro"] = distro
return &config
}
// FS implements the [opctx.FileSystemFactory] interface.
func (e *TestEnv) FS() opctx.FS {
return e.TestFS
}
// FS implements the [opctx.OSEnvFactory] interface.
func (e *TestEnv) OSEnv() opctx.OSEnv {
return e.TestOSEnv
}