-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from pdrum/protect-output-on-err
bug: Prevent wiping down output file on error.
- Loading branch information
Showing
3 changed files
with
96 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package out | ||
|
||
import ( | ||
"os" | ||
"path" | ||
) | ||
|
||
// LazyFile is an io.WriteCloser which defers creation of the file it is supposed to write in | ||
// till the first call to its write function in order to prevent creation of file, if no write | ||
// is supposed to happen. | ||
type LazyFile struct { | ||
FileName string | ||
written []byte | ||
file *os.File | ||
} | ||
|
||
// Close closes the file if it is created. Returns nil if no file is created. | ||
func (lw *LazyFile) Close() error { | ||
if lw.file != nil { | ||
return lw.file.Close() | ||
} | ||
return nil | ||
} | ||
|
||
// Write writes to the specified file and creates the file first time it is called. | ||
func (lw *LazyFile) Write(p []byte) (int, error) { | ||
if lw.file == nil { | ||
err := os.MkdirAll(path.Dir(lw.FileName), 0755) | ||
if err != nil { | ||
return 0, err | ||
} | ||
lw.file, err = os.Create(lw.FileName) | ||
if err != nil { | ||
return 0, err | ||
} | ||
} | ||
return lw.file.Write(p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package out_test | ||
|
||
import ( | ||
"github.com/cheekybits/genny/out" | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
) | ||
|
||
const testFileName = "test-file.go" | ||
|
||
func tearDown() { | ||
var err = os.Remove(testFileName) | ||
if err != nil && !os.IsNotExist(err) { | ||
panic("Could not delete test file") | ||
} | ||
} | ||
|
||
func assertFileContains(t *testing.T, expected string) { | ||
file, err := os.Open(testFileName) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fileBytes, err := ioutil.ReadAll(file) | ||
if err != nil { | ||
panic(err) | ||
} | ||
assert.Equal(t, expected, string(fileBytes), "File contents not written properly") | ||
} | ||
|
||
func TestMultipleWrites(t *testing.T) { | ||
defer tearDown() | ||
lf := out.LazyFile{FileName: testFileName} | ||
defer lf.Close() | ||
lf.Write([]byte("Word1")) | ||
lf.Write([]byte("Word2")) | ||
assertFileContains(t, "Word1Word2") | ||
} | ||
|
||
func TestNoWrite(t *testing.T) { | ||
defer tearDown() | ||
lf := out.LazyFile{FileName: testFileName} | ||
defer lf.Close() | ||
_, err := os.Stat(testFileName) | ||
assert.True(t, os.IsNotExist(err), "Expected file not to be created") | ||
} |