Skip to content

Commit 1d70088

Browse files
jaredfholgateCopilotmatt-FFFFFF
authored
fix: temp folder path (#28)
Resolves a bug where temp folder path is not correctly mapped to outstanding commands in certain scenarios. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Matt White <16320656+matt-FFFFFF@users.noreply.github.com>
1 parent 3c5e465 commit 1d70088

38 files changed

Lines changed: 231 additions & 77 deletions

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ linters:
3535
- unparam
3636
- unused
3737
- wrapcheck
38-
- wsl
38+
- wsl_v5
3939

4040
exclusions:
4141
rules:

DEVELOPER.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,36 @@ Contributions are welcome! Here's how to get started:
118118

119119
6. **Open a Pull Request**
120120

121+
### Debugging
122+
123+
- Most example commands are bash based, so make sure you are running in an environment that supports bash. E.g. on Windows open VSCode from WSL
124+
- Create a launch.json file like this:
125+
126+
```json
127+
{
128+
"version": "0.2.0",
129+
"configurations": [
130+
{
131+
"name": "Launch Package",
132+
"type": "go",
133+
"request": "launch",
134+
"mode": "debug",
135+
"program": "${workspaceRoot}/cmd/porch/main.go",
136+
"env": { "AVM_TEST_TYPE": "integration" },
137+
"args": [ "run", "-f", "${workspaceRoot}/examples/terraform-test-unit.yaml", "-config-timeout", "10000"],
138+
"cwd": "${workspaceRoot}/../terraform-azurerm-lz-vending"
139+
}
140+
]
141+
}
142+
```
143+
144+
- `args`: The is the porch config you want to debug
145+
- `cwd`: This the target folder structure you want to run the command against
146+
- `env`: This is any environment variables needed by your porch config
147+
- Set your break points and Run the VSCode debugger
148+
149+
>NOTE: If you see a Delve related issue, try installing the latest version from trunk: `go install -v github.com/go-delve/delve/cmd/dlv@master`
150+
121151
### Development Guidelines
122152

123153
- Write comprehensive tests for new features

cmd/porch/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ flow control, parallel processing, and comprehensive error handling.`,
5454
func main() {
5555
ctx, cancel := context.WithCancel(context.Background())
5656
ctx = ctxlog.New(ctx, ctxlog.DefaultLogger)
57+
5758
defer cancel()
5859

5960
sigCh := signalbroker.New(ctx)

cmd/porch/run/run.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ import (
2424
)
2525

2626
const (
27-
fileFlag = "file"
28-
outFlag = "out"
29-
noOutputStdErrFlag = "no-output-stderr"
30-
outputStdOutFlag = "output-stdout"
31-
outputSuccessDetailsFlag = "output-success-details"
32-
parallelismFlag = "parallelism"
33-
tuiFlag = "tui"
34-
writeFlag = "write"
35-
configTimeoutSeconds = 30
36-
cliExitStr = ""
27+
fileFlag = "file"
28+
outFlag = "out"
29+
noOutputStdErrFlag = "no-output-stderr"
30+
outputStdOutFlag = "output-stdout"
31+
outputSuccessDetailsFlag = "output-success-details"
32+
parallelismFlag = "parallelism"
33+
tuiFlag = "tui"
34+
writeFlag = "write"
35+
configTimeoutFlag = "config-timeout"
36+
configTimeoutSecondsDefault = 30
37+
cliExitStr = ""
3738
)
3839

3940
var (
@@ -116,6 +117,13 @@ To save the results to a file, specify the output file name as an argument.
116117
TakesFile: false,
117118
OnlyOnce: true,
118119
},
120+
&cli.IntFlag{
121+
Name: configTimeoutFlag,
122+
Aliases: []string{"timeout"},
123+
Usage: "Set the maximum time in seconds to wait for configuration building. " +
124+
"Defaults to 30 seconds.",
125+
Value: configTimeoutSecondsDefault,
126+
},
119127
},
120128
Action: actionFunc,
121129
}
@@ -138,7 +146,7 @@ func actionFunc(ctx context.Context, cmd *cli.Command) error {
138146
factory := ctx.Value(commands.FactoryContextKey{}).(commands.CommanderFactory)
139147

140148
// Create a timeout context for configuration building
141-
configCtx, configCancel := context.WithTimeout(ctx, configTimeoutSeconds*time.Second)
149+
configCtx, configCancel := context.WithTimeout(ctx, time.Duration(cmd.Int(configTimeoutFlag))*time.Second)
142150
defer configCancel()
143151

144152
runnables := make([]runbatch.Runnable, len(url))

examples/terraform-test-unit.yaml

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,87 @@
1-
name: Terraform unit test
2-
description: Terraform unit tests for root module and submodules
1+
name: Terraform test
2+
description: |
3+
Terraform tests for root module and submodules, please set $AVM_TEST_TYPE to "unit" to run unit tests.
4+
Set $AVM_TEST_TYPE to "integration" to run integration tests.
35
command_groups:
4-
- name: terraform_unit_tests
5-
description: Run Terraform unit tests for root module and submodules
6+
- name: terraform_tests
7+
description: Run Terraform tests for root module and submodules
68
commands:
9+
- type: copycwdtotemp
10+
name: Copy current working directory to temp
11+
cwd: "."
12+
13+
- type: shell
14+
name: Clean Terraform
15+
command_line: |
16+
echo $(pwd) && \
17+
find . -type d -name .terraform | xargs -n1 rm -rf && \
18+
find . -type f -name .terraform.lock.hcl | xargs -n1 rm -f && \
19+
find . -type f -name *.tfstate* | xargs -n1 rm -f
20+
721
- type: shell
8-
name: Check for unit tests
22+
name: Check for AVM_TEST_TYPE env
923
command_line: |
10-
if [ ! -d ./tests/unit ]; then
11-
echo "No unit tests found in $(pwd)"
24+
if [ -z "$AVM_TEST_TYPE" ]; then
25+
echo "AVM_TEST_TYPE is not set. Please set AVM_TEST_TYPE to 'unit' or 'integration'." 1>&2
26+
echo "E.g. AVM_TEST_TYPE=\"unit\" # ... rest of your command" 1>&2
27+
exit 1
28+
fi
29+
30+
- type: shell
31+
name: Check for tests
32+
command_line: |
33+
if [ ! -d ./tests/$AVM_TEST_TYPE ]; then
34+
echo "No $AVM_TEST_TYPE tests found in $(pwd)"
1235
exit 99
1336
fi
1437
skip_exit_codes: [99]
38+
39+
- type: shell
40+
name: Check and Run setup.sh
41+
command_line: |
42+
if [ -f ./tests/$AVM_TEST_TYPE/setup.sh ]; then
43+
if [ ! -x ./tests/$AVM_TEST_TYPE/setup.sh ]; then
44+
echo "Making setup.sh executable"
45+
chmod +x ./tests/$AVM_TEST_TYPE/setup.sh
46+
fi
47+
echo "Running setup.sh"
48+
./tests/$AVM_TEST_TYPE/setup.sh
49+
fi
50+
1551
- type: shell
1652
name: "Terraform Init"
17-
command_line: "terraform init -test-directory ./tests/unit"
53+
command_line: "terraform init -test-directory ./tests/$AVM_TEST_TYPE"
54+
1855
- type: shell
1956
name: "Terraform Test"
20-
command_line: "terraform test -test-directory ./tests/unit"
57+
command_line: "terraform test -test-directory ./tests/$AVM_TEST_TYPE"
2158

22-
commands:
23-
- type: copycwdtotemp
24-
name: Copy current working directory to temp
25-
cwd: "."
59+
- type: shell
60+
name: Clean up
61+
command_line: |
62+
rm -fr $(pwd)
63+
runs_on_condition: always
2664

65+
commands:
2766
- type: shell
28-
name: Clean Terraform
29-
command_line: |
30-
echo $(pwd) && \
31-
find . -type d -name .terraform | xargs -n1 rm -rf && \
32-
find . -type f -name .terraform.lock.hcl | xargs -n1 rm -f && \
33-
find . -type f -name *.tfstate* | xargs -n1 rm -f
67+
name: install terraform
68+
command_line: terraform -version
69+
cwd: "."
3470

3571
- type: parallel
36-
name: Unit tests
72+
name: Terraform tests
3773
env:
3874
TF_IN_AUTOMATION: "1"
3975
commands:
4076
- type: foreachdirectory
4177
name: submodules
78+
depth: 1
4279
working_directory: "./modules"
4380
mode: parallel
44-
command_group: terraform_unit_tests
81+
skip_on_not_exist: true
82+
command_group: terraform_tests
83+
working_directory_strategy: item_relative
4584

4685
- type: serial
4786
name: root module
48-
command_group: terraform_unit_tests
49-
50-
- type: shell
51-
name: Clean up
52-
command_line: |
53-
rm -fr $(pwd)
54-
runs_on_condition: always
87+
command_group: terraform_tests

internal/commandregistry/registry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func (r *Registry) resolveCommandGroupWithDepth(
150150
// Check if we're currently visiting this group (circular dependency)
151151
if visiting[groupName] {
152152
cyclePath := append(path, groupName)
153+
153154
return nil, fmt.Errorf("%w: %s", ErrCircularDependency,
154155
formatCircularDependencyPath(cyclePath))
155156
}

internal/commands/command.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ func (d *BaseDefinition) ToBaseCommand(
8686
d.WorkingDirectory,
8787
parent.GetCwd(),
8888
)
89-
9089
if err != nil {
9190
return nil, errors.Join(ErrPath, err)
9291
}
@@ -130,7 +129,6 @@ func HclCommandToBaseCommand(
130129
hclCommand.WorkingDirectory,
131130
parent.GetCwd(),
132131
)
133-
134132
if err != nil {
135133
return nil, err
136134
}

internal/commands/copycwdtotemp/copyCwdToTemp.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ func New(base *runbatch.BaseCommand) *runbatch.FunctionCommand {
105105
return afero.WriteFile(fs, dstPath, srcFile, info.Mode().Perm())
106106
}
107107
})
108-
109108
if err != nil {
110109
return runbatch.FunctionCommandReturn{
111110
Err: err,

internal/commands/copycwdtotemp/copyCwdToTemp_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func TestCopyCwdToTemp_ErrorHandling(t *testing.T) {
152152
setupFS: func() afero.Fs {
153153
baseFs := afero.NewMemMapFs()
154154
errPath := filepath.Join(tmpDir, "porch_testrun")
155+
155156
return &errorFS{fs: baseFs, errorPath: errPath}
156157
},
157158
testFunc: func(t *testing.T, fs afero.Fs) {
@@ -197,8 +198,10 @@ func TestCopyCwdToTemp_ErrorHandling(t *testing.T) {
197198
if err != nil {
198199
panic(err)
199200
}
201+
200202
testFilePath := filepath.Join(srcDir, "file1.txt")
201203
_ = afero.WriteFile(baseFs, testFilePath, []byte("content"), 0644)
204+
202205
return &errorFS{fs: baseFs, errorPath: testFilePath}
203206
},
204207
testFunc: func(t *testing.T, fs afero.Fs) {
@@ -272,7 +275,6 @@ func TestCopyCwdToTemp_ErrorHandling(t *testing.T) {
272275
require.Len(t, results, 1)
273276
assert.Equal(t, -1, results[0].ExitCode) // FunctionCommand.Run returns -1 for errors
274277
assert.Equal(t, ctx.Err(), results[0].Error)
275-
276278
},
277279
},
278280
}

internal/commands/foreachdirectory/commander_hcl_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ type mockRunnable struct {
8383
func (m *mockRunnable) Run(ctx context.Context) runbatch.Results {
8484
return runbatch.Results{}
8585
}
86-
func (m *mockRunnable) GetLabel() string { return m.label }
87-
func (m *mockRunnable) GetCwd() string { return "/" }
88-
func (m *mockRunnable) SetCwd(cwd string) error { return nil }
89-
func (m *mockRunnable) GetCwdRel() string { return "" }
90-
func (m *mockRunnable) InheritEnv(env map[string]string) {}
91-
func (m *mockRunnable) SetParent(parent runbatch.Runnable) {}
92-
func (m *mockRunnable) GetParent() runbatch.Runnable { return nil }
86+
func (m *mockRunnable) GetLabel() string { return m.label }
87+
func (m *mockRunnable) GetCwd() string { return "/" }
88+
func (m *mockRunnable) SetCwd(cwd string) error { return nil }
89+
func (m *mockRunnable) SetCwdToSpecificAbsolute(cwd string) error { return nil }
90+
func (m *mockRunnable) GetCwdRel() string { return "" }
91+
func (m *mockRunnable) InheritEnv(env map[string]string) {}
92+
func (m *mockRunnable) SetParent(parent runbatch.Runnable) {}
93+
func (m *mockRunnable) GetParent() runbatch.Runnable { return nil }
9394
func (m *mockRunnable) ShouldRun(state runbatch.PreviousCommandStatus) runbatch.ShouldRunAction {
9495
return runbatch.ShouldRunActionRun
9596
}

0 commit comments

Comments
 (0)