Skip to content

Commit

Permalink
Unit path check improvements (#3884)
Browse files Browse the repository at this point in the history
* Add check for path

* Simplified tests

* Lint errors

* Helper update

* Temp path update

* Cleanup

* Fixed failing strict mode test
  • Loading branch information
denis256 authored Feb 14, 2025
1 parent 9b6afa0 commit 9555a9f
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 42 deletions.
4 changes: 4 additions & 0 deletions cli/commands/stack/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func processStackFile(ctx context.Context, opts *options.TerragruntOptions, stac

func isLocal(opts *options.TerragruntOptions, src string) bool {
// check initially if the source is a local file
if util.FileExists(src) {
return true
}

src = filepath.Join(opts.WorkingDir, src)
if util.FileExists(src) {
return true
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/stacks/locals/terragrunt.stack.hcl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
locals {
chicken = "units/chicken"
chick = "units/chick"
repo_path = "${get_repo_root()}"
}

unit "mother" {
Expand All @@ -14,12 +15,12 @@ unit "father" {
}

unit "chick_1" {
source = local.chick
source = "${local.chick}"
path = "chicks/chick-1"
}

unit "chick_2" {
source = local.chick
source = "${local.repo_path}/fixtures/stacks/locals/${local.chick}"
path = "chicks/chick-2"
}

8 changes: 8 additions & 0 deletions test/helpers/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net"
"net/http"
"net/url"
"os/exec"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -911,3 +912,10 @@ func IsTerragruntProviderCacheEnabled(t *testing.T) bool {

return false
}

func CreateGitRepo(t *testing.T, dir string) {
t.Helper()

commandOutput, err := exec.Command("git", "init", dir).CombinedOutput()
require.NoErrorf(t, err, "Error initializing git repo: %v\n%s", err, string(commandOutput))
}
15 changes: 4 additions & 11 deletions test/integration_destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -86,10 +85,7 @@ func TestDestroyDependentModule(t *testing.T) {
tmpEnvPath, _ := filepath.EvalSymlinks(helpers.CopyEnvironment(t, testFixtureDestroyDependentModule))
rootPath := util.JoinPath(tmpEnvPath, testFixtureDestroyDependentModule)

commandOutput, err := exec.Command("git", "init", rootPath).CombinedOutput()
if err != nil {
t.Fatalf("Error initializing git repo: %v\n%s", err, string(commandOutput))
}
helpers.CreateGitRepo(t, rootPath)
// apply each module in order
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+util.JoinPath(rootPath, "a"))
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+util.JoinPath(rootPath, "b"))
Expand All @@ -102,7 +98,7 @@ func TestDestroyDependentModule(t *testing.T) {
stderr := bytes.Buffer{}

workingDir := util.JoinPath(rootPath, "c")
err = helpers.RunTerragruntCommand(t, "terragrunt destroy -auto-approve --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+workingDir, &stdout, &stderr)
err := helpers.RunTerragruntCommand(t, "terragrunt destroy -auto-approve --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+workingDir, &stdout, &stderr)
require.NoError(t, err)

output := stderr.String()
Expand Down Expand Up @@ -281,10 +277,7 @@ func TestTerragruntSkipConfirmExternalDependencies(t *testing.T) {
})
require.NoError(t, os.Mkdir(filepath.ToSlash("/tmp/external-46521694"), 0755))

output, err := exec.Command("git", "init", tmpEnvPath).CombinedOutput()
if err != nil {
t.Fatalf("Error initializing git repo: %v\n%s", err, string(output))
}
helpers.CreateGitRepo(t, tmpEnvPath)

stdout := bytes.Buffer{}
stderr := bytes.Buffer{}
Expand All @@ -293,7 +286,7 @@ func TestTerragruntSkipConfirmExternalDependencies(t *testing.T) {
oldStdout := os.Stderr
os.Stderr = w

err = helpers.RunTerragruntCommand(t, "terragrunt destroy --terragrunt-working-dir "+testPath, &stdout, &stderr)
err := helpers.RunTerragruntCommand(t, "terragrunt destroy --terragrunt-working-dir "+testPath, &stdout, &stderr)
os.Stderr = oldStdout
require.NoError(t, w.Close())

Expand Down
35 changes: 7 additions & 28 deletions test/integration_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -178,10 +177,7 @@ func TestGetRepoRootCaching(t *testing.T) {
tmpEnvPath, _ := filepath.EvalSymlinks(helpers.CopyEnvironment(t, testFixtureGetRepoRoot))
rootPath := util.JoinPath(tmpEnvPath, testFixtureGetRepoRoot)

gitOutput, err := exec.Command("git", "init", rootPath).CombinedOutput()
if err != nil {
t.Fatalf("Error initializing git repo: %v\n%s", err, string(gitOutput))
}
helpers.CreateGitRepo(t, rootPath)

stdout, stderr, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt run-all plan --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+rootPath)
require.NoError(t, err)
Expand All @@ -198,10 +194,7 @@ func TestGetRepoRoot(t *testing.T) {
tmpEnvPath, _ := filepath.EvalSymlinks(helpers.CopyEnvironment(t, testFixtureGetRepoRoot))
rootPath := util.JoinPath(tmpEnvPath, testFixtureGetRepoRoot)

output, err := exec.Command("git", "init", rootPath).CombinedOutput()
if err != nil {
t.Fatalf("Error initializing git repo: %v\n%s", err, string(output))
}
helpers.CreateGitRepo(t, rootPath)
helpers.RunTerragrunt(t, "terragrunt apply-all --terragrunt-non-interactive --terragrunt-working-dir "+rootPath)

// verify expected outputs are not empty
Expand Down Expand Up @@ -230,10 +223,7 @@ func TestGetWorkingDirBuiltInFunc(t *testing.T) {
tmpEnvPath, _ := filepath.EvalSymlinks(helpers.CopyEnvironment(t, testFixtureGetWorkingDir))
rootPath := util.JoinPath(tmpEnvPath, testFixtureGetWorkingDir)

output, err := exec.Command("git", "init", rootPath).CombinedOutput()
if err != nil {
t.Fatalf("Error initializing git repo: %v\n%s", err, string(output))
}
helpers.CreateGitRepo(t, rootPath)
helpers.RunTerragrunt(t, "terragrunt apply-all --terragrunt-non-interactive --terragrunt-working-dir "+rootPath)

// verify expected outputs are not empty
Expand All @@ -254,7 +244,7 @@ func TestGetWorkingDirBuiltInFunc(t *testing.T) {
expectedWorkingDir := filepath.Join(rootPath, util.TerragruntCacheDir)
curWalkStep := 0

err = filepath.Walk(expectedWorkingDir,
err := filepath.Walk(expectedWorkingDir,
func(path string, info os.FileInfo, err error) error {
if err != nil || !info.IsDir() {
return err
Expand Down Expand Up @@ -299,11 +289,7 @@ func TestPathRelativeFromInclude(t *testing.T) {
basePath := util.JoinPath(rootPath, "base")
clusterPath := util.JoinPath(rootPath, "cluster")

output, err := exec.Command("git", "init", tmpEnvPath).CombinedOutput()
if err != nil {
t.Fatalf("Error initializing git repo: %v\n%s", err, string(output))
}

helpers.CreateGitRepo(t, tmpEnvPath)
helpers.RunTerragrunt(t, "terragrunt run-all apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+rootPath)

// verify expected outputs are not empty
Expand Down Expand Up @@ -331,11 +317,7 @@ func TestGetPathFromRepoRoot(t *testing.T) {
tmpEnvPath, _ := filepath.EvalSymlinks(helpers.CopyEnvironment(t, testFixtureGetPathFromRepoRoot))
rootPath := util.JoinPath(tmpEnvPath, testFixtureGetPathFromRepoRoot)

output, err := exec.Command("git", "init", tmpEnvPath).CombinedOutput()
if err != nil {
t.Fatalf("Error initializing git repo: %v\n%s", err, string(output))
}

helpers.CreateGitRepo(t, tmpEnvPath)
helpers.RunTerragrunt(t, "terragrunt apply-all --terragrunt-non-interactive --terragrunt-working-dir "+rootPath)

// verify expected outputs are not empty
Expand Down Expand Up @@ -364,10 +346,7 @@ func TestGetPathToRepoRoot(t *testing.T) {
rootPath := util.JoinPath(tmpEnvPath, testFixtureGetPathToRepoRoot)
helpers.CleanupTerraformFolder(t, rootPath)

output, err := exec.Command("git", "init", tmpEnvPath).CombinedOutput()
if err != nil {
t.Fatalf("Error initializing git repo: %v\n%s", err, string(output))
}
helpers.CreateGitRepo(t, tmpEnvPath)
helpers.RunTerragrunt(t, "terragrunt apply-all --terragrunt-non-interactive --terragrunt-working-dir "+rootPath)

// verify expected outputs are not empty
Expand Down
1 change: 1 addition & 0 deletions test/integration_stacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestStacksGenerateLocals(t *testing.T) {

helpers.CleanupTerraformFolder(t, testFixtureStacksLocals)
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureStacksLocals)
helpers.CreateGitRepo(t, tmpEnvPath)
rootPath := util.JoinPath(tmpEnvPath, testFixtureStacksLocals)

helpers.RunTerragrunt(t, "terragrunt stack generate --experiment stacks --terragrunt-working-dir "+rootPath)
Expand Down
2 changes: 1 addition & 1 deletion test/integration_strict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestStrictMode(t *testing.T) {
name: "plan-all",
controls: []string{},
strictMode: false,
expectedStderr: "The `plan-all` command is deprecated and will be removed in a future version. Use `terragrunt run-all plan` instead.",
expectedStderr: "The `plan-all` command is deprecated and will be removed in a future version of Terragrunt. Use `terragrunt run-all plan` instead.",
expectedError: nil,
},
{
Expand Down

0 comments on commit 9555a9f

Please sign in to comment.