Skip to content

Commit 9ac76de

Browse files
authored
Merge pull request #41 from LandonTClipp/mode
Changing WriteFile and OpenFile to use default modes
2 parents b0b2b7d + 547bee6 commit 9ac76de

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

path.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,18 @@ func (p *Path) Open() (*File, error) {
129129
}, err
130130
}
131131

132-
// OpenFile opens a file using the given flags and the given mode.
132+
// OpenFile opens a file using the given flags.
133133
// See the list of flags at: https://golang.org/pkg/os/#pkg-constants
134-
func (p *Path) OpenFile(flag int, perm os.FileMode) (*File, error) {
134+
func (p *Path) OpenFile(flag int) (*File, error) {
135+
handle, err := p.Fs().OpenFile(p.String(), flag, p.DefaultFileMode)
136+
return &File{
137+
File: handle,
138+
}, err
139+
}
140+
141+
// OpenFileMode opens a file using the given flags and the given mode.
142+
// See the list of flags at: https://golang.org/pkg/os/#pkg-constants
143+
func (p *Path) OpenFileMode(flag int, perm os.FileMode) (*File, error) {
135144
handle, err := p.Fs().OpenFile(p.String(), flag, perm)
136145
return &File{
137146
File: handle,
@@ -254,13 +263,20 @@ func (p *Path) SafeWriteReader(r io.Reader) error {
254263
return afero.SafeWriteReader(p.Fs(), p.String(), r)
255264
}
256265

257-
// WriteFile writes the given data to the path (if possible). If the file exists,
266+
// WriteFileMode writes the given data to the path (if possible). If the file exists,
258267
// the file is truncated. If the file is a directory, or the path doesn't exist,
259268
// an error is returned.
260-
func (p *Path) WriteFile(data []byte, perm os.FileMode) error {
269+
func (p *Path) WriteFileMode(data []byte, perm os.FileMode) error {
261270
return afero.WriteFile(p.Fs(), p.String(), data, perm)
262271
}
263272

273+
// WriteFile writes the given data to the path (if possible). If the file exists,
274+
// the file is truncated. If the file is a directory, or the path doesn't exist,
275+
// an error is returned.
276+
func (p *Path) WriteFile(data []byte) error {
277+
return afero.WriteFile(p.Fs(), p.String(), data, p.DefaultFileMode)
278+
}
279+
264280
// WriteReader takes a reader and writes the content
265281
func (p *Path) WriteReader(r io.Reader) error {
266282
return afero.WriteReader(p.Fs(), p.String(), r)

path_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ func (p *PathSuite) TestJoin() {
6060
func (p *PathSuite) TestWriteAndRead() {
6161
expectedBytes := []byte("hello world!")
6262
file := p.tmpdir.Join("test.txt")
63-
require.NoError(p.T(), file.WriteFile(expectedBytes, 0o755))
63+
require.NoError(p.T(), file.WriteFile(expectedBytes))
6464
bytes, err := file.ReadFile()
6565
require.NoError(p.T(), err)
6666
assert.Equal(p.T(), expectedBytes, bytes)
6767
}
6868

6969
func (p *PathSuite) TestChmod() {
7070
file := p.tmpdir.Join("file1.txt")
71-
require.NoError(p.T(), file.WriteFile([]byte(""), 0o755))
71+
require.NoError(p.T(), file.WriteFile([]byte("")))
7272

7373
file.Chmod(0o777)
7474
fileInfo, err := file.Stat()
@@ -114,7 +114,7 @@ func (p *PathSuite) TestMkdirAllMultipleSubdirs() {
114114

115115
func (p *PathSuite) TestRenameString() {
116116
file := p.tmpdir.Join("file.txt")
117-
require.NoError(p.T(), file.WriteFile([]byte("hello world!"), 0o755))
117+
require.NoError(p.T(), file.WriteFile([]byte("hello world!")))
118118

119119
newPath := p.tmpdir.Join("file2.txt")
120120

@@ -137,7 +137,7 @@ func (p *PathSuite) TestRenameString() {
137137

138138
func (p *PathSuite) TestSizeZero() {
139139
file := p.tmpdir.Join("file.txt")
140-
require.NoError(p.T(), file.WriteFile([]byte{}, 0o644))
140+
require.NoError(p.T(), file.WriteFile([]byte{}))
141141
size, err := file.Size()
142142
require.NoError(p.T(), err)
143143
p.Zero(size)
@@ -146,7 +146,7 @@ func (p *PathSuite) TestSizeZero() {
146146
func (p *PathSuite) TestSizeNonZero() {
147147
msg := "oh, it's you"
148148
file := p.tmpdir.Join("file.txt")
149-
require.NoError(p.T(), file.WriteFile([]byte(msg), 0o644))
149+
require.NoError(p.T(), file.WriteFile([]byte(msg)))
150150
size, err := file.Size()
151151
require.NoError(p.T(), err)
152152
p.Equal(len(msg), int(size))
@@ -162,7 +162,7 @@ func (p *PathSuite) TestIsDir() {
162162

163163
func (p *PathSuite) TestIsntDir() {
164164
file := p.tmpdir.Join("file.txt")
165-
require.NoError(p.T(), file.WriteFile([]byte("hello world!"), 0o644))
165+
require.NoError(p.T(), file.WriteFile([]byte("hello world!")))
166166
isDir, err := file.IsDir()
167167
require.NoError(p.T(), err)
168168
p.False(isDir)
@@ -172,7 +172,7 @@ func (p *PathSuite) TestGetLatest() {
172172
now := time.Now()
173173
for i := 0; i < 5; i++ {
174174
file := p.tmpdir.Join(fmt.Sprintf("file%d.txt", i))
175-
require.NoError(p.T(), file.WriteFile([]byte(fmt.Sprintf("hello %d", i)), 0o644))
175+
require.NoError(p.T(), file.WriteFile([]byte(fmt.Sprintf("hello %d", i))))
176176
require.NoError(p.T(), file.Chtimes(now, now))
177177
now = now.Add(time.Duration(1) * time.Hour)
178178
}
@@ -192,7 +192,7 @@ func (p *PathSuite) TestGetLatestEmpty() {
192192
func (p *PathSuite) TestOpen() {
193193
msg := "cubs > cardinals"
194194
file := p.tmpdir.Join("file.txt")
195-
require.NoError(p.T(), file.WriteFile([]byte(msg), 0o644))
195+
require.NoError(p.T(), file.WriteFile([]byte(msg)))
196196
fileHandle, err := file.Open()
197197
require.NoError(p.T(), err)
198198

@@ -204,7 +204,7 @@ func (p *PathSuite) TestOpen() {
204204

205205
func (p *PathSuite) TestOpenFile() {
206206
file := p.tmpdir.Join("file.txt")
207-
fileHandle, err := file.OpenFile(os.O_RDWR|os.O_CREATE, 0o644)
207+
fileHandle, err := file.OpenFile(os.O_RDWR | os.O_CREATE)
208208
require.NoError(p.T(), err)
209209

210210
msg := "do you play croquet?"
@@ -234,7 +234,7 @@ func (p *PathSuite) TestDirExists() {
234234
func (p *PathSuite) TestIsFile() {
235235
file1 := p.tmpdir.Join("file.txt")
236236

237-
require.NoError(p.T(), file1.WriteFile([]byte(""), 0o644))
237+
require.NoError(p.T(), file1.WriteFile([]byte("")))
238238
exists, err := file1.IsFile()
239239
require.NoError(p.T(), err)
240240
p.True(exists)
@@ -243,15 +243,15 @@ func (p *PathSuite) TestIsFile() {
243243
func (p *PathSuite) TestIsEmpty() {
244244
file1 := p.tmpdir.Join("file.txt")
245245

246-
require.NoError(p.T(), file1.WriteFile([]byte(""), 0o644))
246+
require.NoError(p.T(), file1.WriteFile([]byte("")))
247247
isEmpty, err := file1.IsEmpty()
248248
require.NoError(p.T(), err)
249249
p.True(isEmpty)
250250
}
251251

252252
func (p *PathSuite) TestIsSymlink() {
253253
file1 := p.tmpdir.Join("file.txt")
254-
require.NoError(p.T(), file1.WriteFile([]byte(""), 0o644))
254+
require.NoError(p.T(), file1.WriteFile([]byte("")))
255255

256256
symlink := p.tmpdir.Join("symlink")
257257
p.NoError(symlink.Symlink(file1))
@@ -303,7 +303,7 @@ func (p *PathSuite) TestEquals() {
303303

304304
func (p *PathSuite) TestDeepEquals() {
305305
hello := p.tmpdir.Join("hello.txt")
306-
require.NoError(p.T(), hello.WriteFile([]byte("hello"), 0o644))
306+
require.NoError(p.T(), hello.WriteFile([]byte("hello")))
307307
symlink := p.tmpdir.Join("symlink")
308308
require.NoError(p.T(), symlink.Symlink(hello))
309309

@@ -337,10 +337,10 @@ func (p *PathSuite) TestCreate() {
337337

338338
func (p *PathSuite) TestGlobFunction() {
339339
hello1 := p.tmpdir.Join("hello1.txt")
340-
require.NoError(p.T(), hello1.WriteFile([]byte("hello"), 0o644))
340+
require.NoError(p.T(), hello1.WriteFile([]byte("hello")))
341341

342342
hello2 := p.tmpdir.Join("hello2.txt")
343-
require.NoError(p.T(), hello2.WriteFile([]byte("hello2"), 0o644))
343+
require.NoError(p.T(), hello2.WriteFile([]byte("hello2")))
344344

345345
paths, err := Glob(p.tmpdir.Fs(), p.tmpdir.Join("hello1*").String())
346346
p.NoError(err)

scenarios_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import "fmt"
77

88
func HelloWorld(root *Path) error {
99
hello := root.Join("hello.txt")
10-
return hello.WriteFile([]byte("hello world"), 0o644)
10+
return hello.WriteFile([]byte("hello world"))
1111
}
1212

1313
func OneFile(root *Path, name string, content string) error {
1414
file := root.Join(name)
15-
return file.WriteFile([]byte(content), 0o644)
15+
return file.WriteFile([]byte(content))
1616
}
1717

1818
func NFiles(root *Path, n int) error {

walk_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (w *WalkSuiteAll) TestWalkFuncErr() {
111111

112112
func (w *WalkSuiteAll) TestPassesQuerySpecification() {
113113
file := w.root.Join("file.txt")
114-
require.NoError(w.T(), file.WriteFile([]byte("hello"), 0o644))
114+
require.NoError(w.T(), file.WriteFile([]byte("hello")))
115115

116116
stat, err := file.Stat()
117117
require.NoError(w.T(), err)

0 commit comments

Comments
 (0)