diff --git a/internal/ignorefiles/terraformignore.go b/internal/ignorefiles/terraformignore.go index 0ed3ac7..df7d569 100644 --- a/internal/ignorefiles/terraformignore.go +++ b/internal/ignorefiles/terraformignore.go @@ -58,7 +58,6 @@ func readRules(input io.Reader) ([]rule, error) { pattern = "**" + string(os.PathSeparator) + pattern } rule.val = pattern - rule.dirs = strings.Split(pattern, string(os.PathSeparator)) rules = append(rules, rule) currentRuleIndex += 1 } @@ -73,7 +72,6 @@ type rule struct { val string // the value of the rule itself negated bool // prefixed by !, a negated rule negationsAfter bool // negatied rules appear after this rule - dirs []string // directories of the rule regex *regexp.Regexp // regular expression to match for the rule } @@ -173,16 +171,20 @@ func (r *rule) compile() error { var defaultExclusions = []rule{ { - val: strings.Join([]string{"**", ".git", "**"}, string(os.PathSeparator)), - negated: false, + val: strings.Join([]string{"**", ".terraform", "**"}, string(os.PathSeparator)), + negated: false, + negationsAfter: true, }, + // Place negation rules as high as possible in the list { - val: strings.Join([]string{"**", ".terraform", "**"}, string(os.PathSeparator)), - negated: false, + val: strings.Join([]string{"**", ".terraform", "modules", "**"}, string(os.PathSeparator)), + negated: true, + negationsAfter: false, }, { - val: strings.Join([]string{"**", ".terraform", "modules", "**"}, string(os.PathSeparator)), - negated: true, + val: strings.Join([]string{"**", ".git", "**"}, string(os.PathSeparator)), + negated: false, + negationsAfter: false, }, } diff --git a/slug_test.go b/slug_test.go index 3abbc03..c5eac8b 100644 --- a/slug_test.go +++ b/slug_test.go @@ -33,6 +33,39 @@ func TestPack(t *testing.T) { assertArchiveFixture(t, slug, meta) } +func TestPack_defaultRulesOnly(t *testing.T) { + slug := bytes.NewBuffer(nil) + meta, err := Pack("testdata/archive-dir-defaults-only", slug, true) + if err != nil { + t.Fatalf("err: %v", err) + } + + // Make sure .terraform/modules/** are included. + subModuleDir := false + + // Make sure .terraform/plugins are excluded. + pluginsDir := false + + for _, file := range meta.Files { + if strings.HasPrefix(file, filepath.Clean(".terraform/modules/subdir/README")) { + subModuleDir = true + continue + } + + if strings.HasPrefix(file, filepath.Clean(".terraform/plugins/")) { + pluginsDir = true + continue + } + } + if !subModuleDir { + t.Fatal("expected to include .terraform/modules/subdir/README") + } + + if pluginsDir { + t.Fatal("expected to exclude .terraform/plugins") + } +} + func TestPack_rootIsSymlink(t *testing.T) { for _, path := range []string{ "testdata/archive-dir", diff --git a/testdata/archive-dir-defaults-only/.terraform/modules/README b/testdata/archive-dir-defaults-only/.terraform/modules/README new file mode 100644 index 0000000..bc16944 --- /dev/null +++ b/testdata/archive-dir-defaults-only/.terraform/modules/README @@ -0,0 +1 @@ +Keep this file and directory here to test if its properly ignored diff --git a/testdata/archive-dir-defaults-only/.terraform/modules/subdir/README b/testdata/archive-dir-defaults-only/.terraform/modules/subdir/README new file mode 100644 index 0000000..bc16944 --- /dev/null +++ b/testdata/archive-dir-defaults-only/.terraform/modules/subdir/README @@ -0,0 +1 @@ +Keep this file and directory here to test if its properly ignored diff --git a/testdata/archive-dir-defaults-only/.terraform/plugins/foo.txt b/testdata/archive-dir-defaults-only/.terraform/plugins/foo.txt new file mode 100644 index 0000000..c2d32e1 --- /dev/null +++ b/testdata/archive-dir-defaults-only/.terraform/plugins/foo.txt @@ -0,0 +1 @@ +This file should be ignored diff --git a/testdata/archive-dir-defaults-only/bar.txt b/testdata/archive-dir-defaults-only/bar.txt new file mode 100644 index 0000000..5716ca5 --- /dev/null +++ b/testdata/archive-dir-defaults-only/bar.txt @@ -0,0 +1 @@ +bar