From d2880d9d9e20bbe019f02b0477a5ce74c5fc85f2 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sun, 28 Apr 2019 20:14:35 -0400 Subject: Fixing file-mode inheritance bug Signed-off-by: Dave Henderson --- template.go | 7 ++++--- template_unix_test.go | 4 ++-- tests/integration/inputdir_test.go | 38 ++++++++++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/template.go b/template.go index 78736b52..7022664b 100644 --- a/template.go +++ b/template.go @@ -177,12 +177,13 @@ func walkDir(dir string, outFileNamer func(string) (string, error), excludeGlob return nil, err } + fMode := mode if mode == 0 { stat, perr := fs.Stat(nextInPath) if perr == nil { - mode = stat.Mode() + fMode = stat.Mode() } else { - mode = dirMode + fMode = dirMode } } @@ -194,7 +195,7 @@ func walkDir(dir string, outFileNamer func(string) (string, error), excludeGlob templates = append(templates, &tplate{ name: nextInPath, targetPath: nextOutPath, - mode: mode, + mode: fMode, modeOverride: modeOverride, }) } diff --git a/template_unix_test.go b/template_unix_test.go index 930b2835..63ea1998 100644 --- a/template_unix_test.go +++ b/template_unix_test.go @@ -21,7 +21,7 @@ func TestWalkDir(t *testing.T) { _ = fs.MkdirAll("/indir/one", 0777) _ = fs.MkdirAll("/indir/two", 0777) afero.WriteFile(fs, "/indir/one/foo", []byte("foo"), 0644) - afero.WriteFile(fs, "/indir/one/bar", []byte("bar"), 0644) + afero.WriteFile(fs, "/indir/one/bar", []byte("bar"), 0664) afero.WriteFile(fs, "/indir/two/baz", []byte("baz"), 0644) templates, err := walkDir("/indir", simpleNamer("/outdir"), []string{"*/two"}, 0, false) @@ -31,7 +31,7 @@ func TestWalkDir(t *testing.T) { { name: "/indir/one/bar", targetPath: "/outdir/one/bar", - mode: 0644, + mode: 0664, }, { name: "/indir/one/foo", diff --git a/tests/integration/inputdir_test.go b/tests/integration/inputdir_test.go index 341aab57..1e0bfb6a 100644 --- a/tests/integration/inputdir_test.go +++ b/tests/integration/inputdir_test.go @@ -24,7 +24,7 @@ var _ = Suite(&InputDirSuite{}) func (s *InputDirSuite) SetUpTest(c *C) { s.tmpDir = fs.NewDir(c, "gomplate-inttests", fs.WithFile("config.yml", "one: eins\ntwo: deux\n"), - fs.WithFile("filemap.json", `{"eins.txt":"uno","deux.txt":"dos"}`), + fs.WithFile("filemap.json", `{"eins.txt":"uno","deux.txt":"dos","drei.sh":"tres","vier.txt":"quatro"}`), fs.WithFile("out.t", `{{- /* .in may contain a directory name - we want to preserve that */ -}} {{ $f := filepath.Base .in -}} out/{{ .in | strings.ReplaceAll $f (index .filemap $f) }}.out @@ -34,6 +34,8 @@ out/{{ .in | strings.ReplaceAll $f (index .filemap $f) }}.out fs.WithDir("inner", fs.WithFile("deux.txt", `{{ (ds "config").two }}`, fs.WithMode(0444)), ), + fs.WithFile("drei.sh", `#!/bin/sh\necho "hello world"\n`, fs.WithMode(0755)), + fs.WithFile("vier.txt", `{{ (ds "config").two }} * {{ (ds "config").two }}`, fs.WithMode(0544)), ), fs.WithDir("out"), fs.WithDir("bad_in", @@ -56,7 +58,7 @@ func (s *InputDirSuite) TestInputDir(c *C) { files, err := ioutil.ReadDir(s.tmpDir.Join("out")) assert.NilError(c, err) - tassert.Len(c, files, 2) + tassert.Len(c, files, 4) files, err = ioutil.ReadDir(s.tmpDir.Join("out", "inner")) assert.NilError(c, err) @@ -68,14 +70,16 @@ func (s *InputDirSuite) TestInputDir(c *C) { content string }{ {s.tmpDir.Join("out", "eins.txt"), 0644, "eins"}, - {s.tmpDir.Join("out", "inner", "deux.txt"), 0644, "deux"}, + {s.tmpDir.Join("out", "inner", "deux.txt"), 0444, "deux"}, + {s.tmpDir.Join("out", "drei.sh"), 0755, `#!/bin/sh\necho "hello world"\n`}, + {s.tmpDir.Join("out", "vier.txt"), 0544, "deux * deux"}, } for _, v := range testdata { info, err := os.Stat(v.path) assert.NilError(c, err) // chmod support on Windows is pretty weak for now if runtime.GOOS != "windows" { - assert.Equal(c, v.mode, info.Mode()) + assert.Equal(c, v.mode, info.Mode(), v.path) } content, err := ioutil.ReadFile(v.path) assert.NilError(c, err) @@ -94,7 +98,7 @@ func (s *InputDirSuite) TestInputDirWithModeOverride(c *C) { files, err := ioutil.ReadDir(s.tmpDir.Join("out")) assert.NilError(c, err) - tassert.Len(c, files, 2) + tassert.Len(c, files, 4) files, err = ioutil.ReadDir(s.tmpDir.Join("out", "inner")) assert.NilError(c, err) @@ -107,6 +111,8 @@ func (s *InputDirSuite) TestInputDirWithModeOverride(c *C) { }{ {s.tmpDir.Join("out", "eins.txt"), 0601, "eins"}, {s.tmpDir.Join("out", "inner", "deux.txt"), 0601, "deux"}, + {s.tmpDir.Join("out", "drei.sh"), 0601, `#!/bin/sh\necho "hello world"\n`}, + {s.tmpDir.Join("out", "vier.txt"), 0601, "deux * deux"}, } for _, v := range testdata { info, err := os.Stat(v.path) @@ -133,7 +139,7 @@ func (s *InputDirSuite) TestOutputMapInline(c *C) { files, err := ioutil.ReadDir(s.tmpDir.Join("OUT")) assert.NilError(c, err) - tassert.Len(c, files, 2) + tassert.Len(c, files, 4) files, err = ioutil.ReadDir(s.tmpDir.Join("OUT", "INNER")) assert.NilError(c, err) @@ -145,7 +151,9 @@ func (s *InputDirSuite) TestOutputMapInline(c *C) { content string }{ {s.tmpDir.Join("OUT", "EINS.TXT"), 0644, "eins"}, - {s.tmpDir.Join("OUT", "INNER", "DEUX.TXT"), 0644, "deux"}, + {s.tmpDir.Join("OUT", "INNER", "DEUX.TXT"), 0444, "deux"}, + {s.tmpDir.Join("OUT", "DREI.SH"), 0755, `#!/bin/sh\necho "hello world"\n`}, + {s.tmpDir.Join("OUT", "VIER.TXT"), 0544, "deux * deux"}, } for _, v := range testdata { info, err := os.Stat(v.path) @@ -174,7 +182,7 @@ func (s *InputDirSuite) TestOutputMapExternal(c *C) { files, err := ioutil.ReadDir(s.tmpDir.Join("out")) assert.NilError(c, err) - tassert.Len(c, files, 2) + tassert.Len(c, files, 4) files, err = ioutil.ReadDir(s.tmpDir.Join("out", "inner")) assert.NilError(c, err) @@ -186,7 +194,9 @@ func (s *InputDirSuite) TestOutputMapExternal(c *C) { content string }{ {s.tmpDir.Join("out", "uno.out"), 0644, "eins"}, - {s.tmpDir.Join("out", "inner", "dos.out"), 0644, "deux"}, + {s.tmpDir.Join("out", "inner", "dos.out"), 0444, "deux"}, + {s.tmpDir.Join("out", "tres.out"), 0755, `#!/bin/sh\necho "hello world"\n`}, + {s.tmpDir.Join("out", "quatro.out"), 0544, "deux * deux"}, } for _, v := range testdata { info, err := os.Stat(v.path) @@ -212,7 +222,7 @@ func (s *InputDirSuite) TestDefaultOutputDir(c *C) { files, err := ioutil.ReadDir(s.tmpDir.Join("out")) assert.NilError(c, err) - tassert.Len(c, files, 2) + tassert.Len(c, files, 4) files, err = ioutil.ReadDir(s.tmpDir.Join("out", "inner")) assert.NilError(c, err) @@ -225,6 +235,14 @@ func (s *InputDirSuite) TestDefaultOutputDir(c *C) { content, err = ioutil.ReadFile(s.tmpDir.Join("out", "inner", "deux.txt")) assert.NilError(c, err) assert.Equal(c, "deux", string(content)) + + content, err = ioutil.ReadFile(s.tmpDir.Join("out", "drei.sh")) + assert.NilError(c, err) + assert.Equal(c, `#!/bin/sh\necho "hello world"\n`, string(content)) + + content, err = ioutil.ReadFile(s.tmpDir.Join("out", "vier.txt")) + assert.NilError(c, err) + assert.Equal(c, `deux * deux`, string(content)) } func (s *InputDirSuite) TestReportsFilenameWithBadInputFile(c *C) { -- cgit v1.2.3