Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore deleted directories not part of a stack in change detection #287

Merged
merged 8 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 87 additions & 4 deletions cmd/terramate/e2etests/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package e2etest

import (
"path/filepath"
"testing"

"github.com/mineiros-io/terramate/config"
Expand Down Expand Up @@ -148,8 +149,7 @@ func TestListDetectChangesInSubDirOfStack(t *testing.T) {
cli := newCLI(t, s.RootDir())

git := s.Git()
git.Add(".")
git.Commit("all")
git.CommitAll("all")
git.Push("main")
git.CheckoutNew("change-the-stack")

Expand Down Expand Up @@ -180,8 +180,7 @@ terramate {
cli := newCLI(t, s.RootDir())

git := s.Git()
git.Add(".")
git.Commit("all")
git.CommitAll("all")
git.Push("main")
git.CheckoutNew("change-the-stack")

Expand All @@ -195,6 +194,90 @@ terramate {
assertRunResult(t, cli.listChangedStacks(), want)
}

func TestListChangedIgnoreDeletedStackDirectory(t *testing.T) {
s := sandbox.New(t)

stack := s.CreateStack("stack-old")
cli := newCLI(t, s.RootDir())

git := s.Git()
git.CommitAll("all")
git.Push("main")
git.CheckoutNew("deleted-stack")

test.RemoveAll(t, stack.Path())

git.CommitAll("removed stack")

assertRun(t, cli.listChangedStacks())
}

func TestListChangedIgnoreDeletedNonStackDirectory(t *testing.T) {
s := sandbox.New(t)

s.CreateStack("stack")
toBeDeletedDir := filepath.Join(s.RootDir(), "to-be-deleted")
test.MkdirAll(t, toBeDeletedDir)
test.WriteFile(t, toBeDeletedDir, "test.txt", "")
cli := newCLI(t, s.RootDir())

git := s.Git()
git.CommitAll("all")
git.Push("main")

git.CheckoutNew("deleted-diretory")

test.RemoveAll(t, toBeDeletedDir)
git.CommitAll("removed directory")

assertRun(t, cli.listChangedStacks())
}

func TestListChangedDontIgnoreStackDeletedFiles(t *testing.T) {
s := sandbox.New(t)

stack := s.CreateStack("stack")
testDir := stack.CreateDir("test")
file := testDir.CreateFile("testfile", "")
cli := newCLI(t, s.RootDir())

git := s.Git()
git.CommitAll("all")
git.Push("main")
git.CheckoutNew("deleted-file")

test.RemoveAll(t, file.Path())

git.CommitAll("removed file")

assertRunResult(t, cli.listChangedStacks(), runExpected{
Stdout: stack.RelPath() + "\n",
})
}

func TestListChangedDontIgnoreStackDeletedDirectories(t *testing.T) {
s := sandbox.New(t)

stack := s.CreateStack("stack")
testDir := stack.CreateDir("test")
testDir.CreateFile("testfile1", "")
testDir.CreateFile("testfile2", "")
cli := newCLI(t, s.RootDir())

git := s.Git()
git.CommitAll("all")
git.Push("main")
git.CheckoutNew("deleted-dir")

test.RemoveAll(t, testDir.Path())

git.CommitAll("removed directory")

assertRunResult(t, cli.listChangedStacks(), runExpected{
Stdout: stack.RelPath() + "\n",
})
}

func TestListTwiceBug(t *testing.T) {
const (
mainTfFileName = "main.tf"
Expand Down
2 changes: 1 addition & 1 deletion hcl/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ func loadCfgBlocks(dir string) (*hclparse.Parser, error) {

dirEntries, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("reading dir to load config files: %v", err)
return nil, fmt.Errorf("reading dir to load config files: %w", err)
}

logger.Trace().Msg("looking for Terramate files")
Expand Down
3 changes: 2 additions & 1 deletion manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package terramate

import (
"errors"
"fmt"
"io/fs"
"os"
Expand Down Expand Up @@ -165,7 +166,7 @@ func (m *Manager) ListChanged() (*StacksReport, error) {
Str("path", dirname).
Msg("Try load changed.")
s, found, err := m.stackLoader.TryLoadChanged(m.root, dirname)
if err != nil {
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("listing changed files: %w", err)
}

Expand Down