Skip to content

Commit 534f29f

Browse files
Copilotdmcilvaney
andauthored
refactor: replace file permission literals with fileperms constants (#65)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dmcilvaney <23200982+dmcilvaney@users.noreply.github.com>
1 parent 62aaa8a commit 534f29f

File tree

11 files changed

+169
-120
lines changed

11 files changed

+169
-120
lines changed

internal/app/azldev/cmds/advanced/mock_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/cmds/advanced"
1212
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/testutils"
1313
"github.com/microsoft/azure-linux-dev-tools/internal/projectconfig"
14+
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms"
1415
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
1516
"github.com/stretchr/testify/assert"
1617
"github.com/stretchr/testify/require"
@@ -128,7 +129,7 @@ func TestBuildRPMS(t *testing.T) {
128129
OutputDirPath: testOutputDirPath,
129130
}
130131

131-
require.NoError(t, fileutils.WriteFile(testEnv.FS(), testMockConfigPath, []byte{}, 0o600))
132+
require.NoError(t, fileutils.WriteFile(testEnv.FS(), testMockConfigPath, []byte{}, fileperms.PrivateFile))
132133

133134
// Confirm that we can "build" RPMs without an explicit mock config file, because
134135
// the loaded project config provides that.

internal/app/azldev/core/sources/overlays_test.go

Lines changed: 130 additions & 89 deletions
Large diffs are not rendered by default.

internal/app/azldev/core/sources/sourceprep_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/microsoft/azure-linux-dev-tools/internal/projectconfig"
1616
"github.com/microsoft/azure-linux-dev-tools/internal/providers/sourceproviders"
1717
"github.com/microsoft/azure-linux-dev-tools/internal/providers/sourceproviders/sourceproviders_test"
18+
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms"
1819
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
1920
"github.com/stretchr/testify/assert"
2021
"github.com/stretchr/testify/require"
@@ -60,7 +61,7 @@ func TestPrepareSources_Success(t *testing.T) {
6061
sourceManager.EXPECT().FetchComponent(gomock.Any(), component, testOutputDir, gomock.Any()).DoAndReturn(
6162
func(_ interface{}, _ interface{}, outputDir string, _ ...sourceproviders.FetchComponentOption) error {
6263
// Create the expected spec file.
63-
return fileutils.WriteFile(ctx.FS(), outputSpecPath, []byte("# test spec"), 0o644)
64+
return fileutils.WriteFile(ctx.FS(), outputSpecPath, []byte("# test spec"), fileperms.PublicFile)
6465
},
6566
)
6667

@@ -184,7 +185,7 @@ func TestPrepareSources_WritesMacrosFile(t *testing.T) {
184185
// Create the expected spec file.
185186
specPath := filepath.Join(outputDir, "my-package.spec")
186187

187-
return fileutils.WriteFile(ctx.FS(), specPath, []byte("# test spec"), 0o644)
188+
return fileutils.WriteFile(ctx.FS(), specPath, []byte("# test spec"), fileperms.PublicFile)
188189
},
189190
)
190191

@@ -441,7 +442,7 @@ Summary: Test component
441442
make test
442443
`
443444

444-
return fileutils.WriteFile(ctx.FS(), outputSpecPath, []byte(specContent), 0o644)
445+
return fileutils.WriteFile(ctx.FS(), outputSpecPath, []byte(specContent), fileperms.PublicFile)
445446
},
446447
)
447448

@@ -498,7 +499,7 @@ Summary: Test component
498499
make test
499500
`
500501

501-
return fileutils.WriteFile(ctx.FS(), outputSpecPath, []byte(specContent), 0o644)
502+
return fileutils.WriteFile(ctx.FS(), outputSpecPath, []byte(specContent), fileperms.PublicFile)
502503
},
503504
)
504505

@@ -536,7 +537,7 @@ func TestDiffSources_NoOverlays(t *testing.T) {
536537
func(_ interface{}, _ interface{}, outputDir string, _ ...sourceproviders.FetchComponentOption) error {
537538
specPath := filepath.Join(outputDir, "test-component.spec")
538539

539-
return fileutils.WriteFile(ctx.FS(), specPath, []byte("Name: test-component\nVersion: 1.0\n"), 0o644)
540+
return fileutils.WriteFile(ctx.FS(), specPath, []byte("Name: test-component\nVersion: 1.0\n"), fileperms.PublicFile)
540541
},
541542
)
542543

@@ -579,7 +580,7 @@ func TestDiffSources_WithOverlays(t *testing.T) {
579580
func(_ interface{}, _ interface{}, outputDir string, _ ...sourceproviders.FetchComponentOption) error {
580581
specPath := filepath.Join(outputDir, "test-component.spec")
581582

582-
return fileutils.WriteFile(ctx.FS(), specPath, []byte("Name: test-component\nVersion: 1.0\n"), 0o644)
583+
return fileutils.WriteFile(ctx.FS(), specPath, []byte("Name: test-component\nVersion: 1.0\n"), fileperms.PublicFile)
583584
},
584585
)
585586

internal/projectconfig/configfile.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/go-playground/validator/v10"
1010
"github.com/microsoft/azure-linux-dev-tools/internal/global/opctx"
11+
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms"
1112
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
1213
"github.com/pelletier/go-toml/v2"
1314
)
@@ -114,7 +115,7 @@ func (f ConfigFile) ToBytes() ([]byte, error) {
114115
// Serializes writes the config file to the specified path in appropriate format (TOML). If the given path already
115116
// exists, it will be overwritten.
116117
func (f ConfigFile) Serialize(fs opctx.FS, filePath string) error {
117-
const defaultPerms = 0o644
118+
const defaultPerms = fileperms.PublicFile
118119

119120
bytes, err := f.ToBytes()
120121
if err != nil {

internal/projectgen/projectgen.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/microsoft/azure-linux-dev-tools/internal/global/opctx"
1515
"github.com/microsoft/azure-linux-dev-tools/internal/projectconfig"
16+
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms"
1617
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
1718
)
1819

@@ -22,7 +23,7 @@ const (
2223
DefaultWorkDir = "build/work"
2324
defaultOutputDir = "out"
2425

25-
defaultFilePerms = 0o644
26+
defaultFilePerms = fileperms.PublicFile
2627
)
2728

2829
// ErrProjectRootAlreadyExists is returned when a project root directory already exists but an attempt

internal/projectgen/projectgen_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TestInitializeProject(t *testing.T) {
127127
// Create the test project path and a config file.
128128
require.NoError(t, fileutils.MkdirAll(env.FS(), testProjectPath))
129129
configFilePath := filepath.Join(testProjectPath, projectconfig.DefaultConfigFileName)
130-
require.NoError(t, fileutils.WriteFile(env.FS(), configFilePath, []byte{}, 0o600))
130+
require.NoError(t, fileutils.WriteFile(env.FS(), configFilePath, []byte{}, fileperms.PrivateFile))
131131

132132
// Run the code-under-test.
133133
err := projectgen.InitializeProject(env.FS(), testProjectPath, &projectgen.NewProjectOptions{})

internal/providers/sourceproviders/fedorasource/fedorasource_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/microsoft/azure-linux-dev-tools/internal/global/testctx"
1313
"github.com/microsoft/azure-linux-dev-tools/internal/utils/downloader/downloader_test"
14+
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms"
1415
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
1516
"github.com/microsoft/azure-linux-dev-tools/internal/utils/retry"
1617
"github.com/spf13/afero"
@@ -24,8 +25,8 @@ const (
2425
testPackageName = "test-package"
2526
testRepoDir = "/test/repo"
2627
testEmptyRepoDir = "/test/empty-repo"
27-
testFilePerms = 0o644
28-
testDirPerms = 0o755
28+
testFilePerms = fileperms.PublicFile
29+
testDirPerms = fileperms.PublicDir
2930

3031
// Test source file data.
3132
testSourcesContent = `SHA512 (example-1.0.tar.gz) = af5ae0eb4ad9c3f07b7d78ec9dfd03f6a00c257df6b829b21051d4ba2d106bf` +
@@ -135,7 +136,7 @@ func TestExtractSourcesFromRepoValidation(t *testing.T) {
135136
})
136137

137138
t.Run("missing sources file", func(t *testing.T) {
138-
require.NoError(t, ctx.FS().MkdirAll(testEmptyRepoDir, 0o755))
139+
require.NoError(t, ctx.FS().MkdirAll(testEmptyRepoDir, fileperms.PublicDir))
139140

140141
// Missing sources file is valid - it means no external sources to download
141142
err := extractor.ExtractSourcesFromRepo(

internal/utils/cloudinit/cloudconfig_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/microsoft/azure-linux-dev-tools/internal/global/testctx"
1111
"github.com/microsoft/azure-linux-dev-tools/internal/utils/cloudinit"
12+
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms"
1213
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
1314
"github.com/spf13/afero"
1415
"github.com/stretchr/testify/assert"
@@ -264,7 +265,7 @@ func TestWriteDataFiles(t *testing.T) {
264265
ctx := testctx.NewCtx()
265266
dir := "/cloud-init"
266267

267-
require.NoError(t, ctx.FS().MkdirAll(dir, 0o755))
268+
require.NoError(t, ctx.FS().MkdirAll(dir, fileperms.PublicDir))
268269

269270
metaDataPath, userDataPath, err := cloudinit.WriteDataFiles(ctx, dir, testCase.hostname, testCase.config)
270271

internal/utils/fileutils/embedfs_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms"
1314
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
1415
"github.com/stretchr/testify/require"
1516
)
@@ -158,13 +159,13 @@ func TestEmbedFSReadOnlyOperations(t *testing.T) {
158159
{
159160
name: "Mkdir",
160161
fn: func() error {
161-
return embedfs.Mkdir("newdir", 0o755)
162+
return embedfs.Mkdir("newdir", fileperms.PublicDir)
162163
},
163164
},
164165
{
165166
name: "MkdirAll",
166167
fn: func() error {
167-
return embedfs.MkdirAll("new/nested/dir", 0o755)
168+
return embedfs.MkdirAll("new/nested/dir", fileperms.PublicDir)
168169
},
169170
},
170171
{
@@ -188,7 +189,7 @@ func TestEmbedFSReadOnlyOperations(t *testing.T) {
188189
{
189190
name: "Chmod",
190191
fn: func() error {
191-
return embedfs.Chmod("testdata/embedfs/file1.txt", 0o644)
192+
return embedfs.Chmod("testdata/embedfs/file1.txt", fileperms.PublicFile)
192193
},
193194
},
194195
{

internal/utils/localrepo/localrepo_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111

1212
"github.com/microsoft/azure-linux-dev-tools/internal/global/testctx"
13+
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms"
1314
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
1415
"github.com/microsoft/azure-linux-dev-tools/internal/utils/localrepo"
1516
"github.com/spf13/afero"
@@ -50,7 +51,7 @@ func TestPublisher_EnsureRepoExists_ExistingDirectory(t *testing.T) {
5051
testFS := afero.NewMemMapFs()
5152
ctx := testctx.NewCtx(testctx.WithFS(testFS))
5253

53-
require.NoError(t, testFS.MkdirAll(testRepoPath, 0o755))
54+
require.NoError(t, testFS.MkdirAll(testRepoPath, fileperms.PublicDir))
5455

5556
publisher, err := localrepo.NewPublisher(ctx, testRepoPath, false)
5657
require.NoError(t, err)
@@ -117,7 +118,7 @@ func TestPublisher_EnsureRepoInitialized_SkipsIfRepodataExists(t *testing.T) {
117118
}
118119

119120
// Pre-create the repo with repodata directory.
120-
require.NoError(t, testFS.MkdirAll(path.Join(testRepoPath, "repodata"), 0o755))
121+
require.NoError(t, testFS.MkdirAll(path.Join(testRepoPath, "repodata"), fileperms.PublicDir))
121122

122123
publisher, err := localrepo.NewPublisher(ctx, testRepoPath, false)
123124
require.NoError(t, err)
@@ -143,17 +144,17 @@ func TestPublisher_PublishRPMs_CopiesFilesAndRunsCreaterepoC(t *testing.T) {
143144
return nil
144145
}
145146

146-
require.NoError(t, testFS.MkdirAll(testRepoPath, 0o755))
147+
require.NoError(t, testFS.MkdirAll(testRepoPath, fileperms.PublicDir))
147148

148149
// Create source RPM files.
149150
sourcePath := "/build/output"
150-
require.NoError(t, testFS.MkdirAll(sourcePath, 0o755))
151+
require.NoError(t, testFS.MkdirAll(sourcePath, fileperms.PublicDir))
151152

152153
rpm1 := path.Join(sourcePath, "package-1.0-1.x86_64.rpm")
153154
rpm2 := path.Join(sourcePath, "package-devel-1.0-1.x86_64.rpm")
154155

155-
require.NoError(t, fileutils.WriteFile(testFS, rpm1, []byte("rpm1 content"), 0o644))
156-
require.NoError(t, fileutils.WriteFile(testFS, rpm2, []byte("rpm2 content"), 0o644))
156+
require.NoError(t, fileutils.WriteFile(testFS, rpm1, []byte("rpm1 content"), fileperms.PublicFile))
157+
require.NoError(t, fileutils.WriteFile(testFS, rpm2, []byte("rpm2 content"), fileperms.PublicFile))
157158

158159
publisher, err := localrepo.NewPublisher(ctx, testRepoPath, false)
159160
require.NoError(t, err)

0 commit comments

Comments
 (0)