From 9f128b42fdbc4acd8473710f9f6c38f6caa07f7b Mon Sep 17 00:00:00 2001 From: Billy Ho <77315748+hohobilly@users.noreply.github.com> Date: Wed, 9 Aug 2023 16:10:14 +0900 Subject: [PATCH] feat: preserve file permission when write formatted files (#1636) test: add a test case to validate permission equal --- format/format.go | 7 +++++++ format/format_test.go | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/format/format.go b/format/format.go index f6ce2ad03..0b9dbfc2d 100644 --- a/format/format.go +++ b/format/format.go @@ -107,6 +107,10 @@ func (f *Format) format(path string) error { } func write(path string, contents []byte) error { + originalFileInfo, err := os.Stat(path) + if err != nil { + return err + } f, err := os.CreateTemp(filepath.Dir(path), filepath.Base(path)) if err != nil { return err @@ -118,5 +122,8 @@ func write(path string, contents []byte) error { if err := f.Close(); err != nil { return err } + if err := os.Chmod(f.Name(), originalFileInfo.Mode()); err != nil { + return err + } return os.Rename(f.Name(), path) } diff --git a/format/format_test.go b/format/format_test.go index 152f6124a..7670e7248 100644 --- a/format/format_test.go +++ b/format/format_test.go @@ -16,6 +16,19 @@ func TestFormat_Format(t *testing.T) { assert.True(t, fx.isFormatted("api/api.go")) } +func TestFormat_PermissionsPreserved(t *testing.T) { + fx := setup(t) + + originalFileInfo, err := os.Stat(filepath.Join(fx.basedir, "main.go")) + if err != nil { + t.Fatal(err) + } + + assert.NoError(t, New().Build(&Config{SearchDir: fx.basedir})) + assert.True(t, permissionsEqual(t, filepath.Join(fx.basedir, "main.go"), originalFileInfo.Mode())) + assert.True(t, permissionsEqual(t, filepath.Join(fx.basedir, "api/api.go"), originalFileInfo.Mode())) +} + func TestFormat_ExcludeDir(t *testing.T) { fx := setup(t) assert.NoError(t, New().Build(&Config{ @@ -96,6 +109,14 @@ func (fx *fixture) isFormatted(file string) bool { return !bytes.Equal(testFiles[file], contents) } +func permissionsEqual(t *testing.T, path string, expectedMode os.FileMode) bool { + fileInfo, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + return expectedMode == fileInfo.Mode() +} + var testFiles = map[string][]byte{ "api/api.go": []byte(`package api