Skip to content
Snippets Groups Projects
Unverified Commit 24bffd5c authored by John Arundel's avatar John Arundel Committed by GitHub
Browse files

Merge pull request #93 from bitfield/fix_truncate_bug_81

Fix #81
parents d4e38fc3 0dd5e2c3
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,7 @@ version: 2
jobs:
test:
docker:
- image: circleci/golang:1.12
- image: circleci/golang:1.17
steps:
- checkout
- run: go test ./...
......
......@@ -861,7 +861,7 @@ fmt.Println(err)
## WriteFile
`WriteFile()` writes the contents of the pipe to a named file. It returns the number of bytes written, or an error:
`WriteFile()` writes the contents of the pipe to a named file, truncating it if it exists. It returns the number of bytes written, or an error:
```go
var wrote int
......
......@@ -105,11 +105,12 @@ func (p *Pipe) String() (string, error) {
}
// WriteFile writes the contents of the Pipe to the specified file, and closes
// the pipe after reading. It returns the number of bytes successfully written,
// or an error. If there is an error reading or writing, the pipe's error status
// is also set.
// the pipe after reading. If the file already exists, it is truncated and the
// new data will replace the old. It returns the number of bytes successfully
// written, or an error. If there is an error reading or writing, the pipe's
// error status is also set.
func (p *Pipe) WriteFile(fileName string) (int64, error) {
return p.writeOrAppendFile(fileName, os.O_RDWR|os.O_CREATE)
return p.writeOrAppendFile(fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC)
}
func (p *Pipe) writeOrAppendFile(fileName string, mode int) (int64, error) {
......
......@@ -2,10 +2,11 @@ package script
import (
"bytes"
"github.com/google/go-cmp/cmp"
"io/ioutil"
"os"
"testing"
"github.com/google/go-cmp/cmp"
)
// doSinksOnPipe calls every kind of sink method on the supplied pipe and
......@@ -61,12 +62,11 @@ func doSinksOnPipe(t *testing.T, p *Pipe, kind string) {
func TestAppendFile(t *testing.T) {
t.Parallel()
orig := "Hello, world"
testFile := "testdata/appendfile.txt"
defer os.Remove(testFile)
path := t.TempDir() + t.Name()
// ignore results; we're testing AppendFile, not WriteFile
_, _ = Echo(orig).WriteFile(testFile)
_, _ = Echo(orig).WriteFile(path)
extra := " and goodbye"
wrote, err := Echo(extra).AppendFile(testFile)
wrote, err := Echo(extra).AppendFile(path)
if err != nil {
t.Error(err)
}
......@@ -74,7 +74,7 @@ func TestAppendFile(t *testing.T) {
t.Errorf("want %d bytes written, got %d", len(extra), int(wrote))
}
// check file contains both contents
got, err := File(testFile).String()
got, err := File(path).String()
if err != nil {
t.Error(err)
}
......@@ -297,19 +297,18 @@ func TestString(t *testing.T) {
}
}
func TestWriteFile(t *testing.T) {
func TestWriteFileNew(t *testing.T) {
t.Parallel()
want := "Hello, world"
testFile := "testdata/writefile.txt"
defer os.Remove(testFile)
wrote, err := Echo(want).WriteFile(testFile)
path := t.TempDir() + t.Name()
wrote, err := Echo(want).WriteFile(path)
if err != nil {
t.Error(err)
}
if int(wrote) != len(want) {
t.Errorf("want %d bytes written, got %d", len(want), int(wrote))
}
got, err := File(testFile).String()
got, err := File(path).String()
if err != nil {
t.Error(err)
}
......@@ -317,3 +316,32 @@ func TestWriteFile(t *testing.T) {
t.Errorf("want %q, got %q", want, got)
}
}
func TestWriteFileTruncatesExisting(t *testing.T) {
t.Parallel()
want := "Hello, world"
path := t.TempDir() + t.Name()
// write some data first so we can check for truncation
data := make([]byte, 15)
err := os.WriteFile(path, data, 0600)
if err != nil {
t.Fatal(err)
}
wrote, err := Echo(want).WriteFile(path)
if err != nil {
t.Error(err)
}
if int(wrote) != len(want) {
t.Errorf("want %d bytes written, got %d", len(want), int(wrote))
}
got, err := File(path).String()
if err != nil {
t.Error(err)
}
if got == want+"\x00\x00\x00" {
t.Fatalf("file not truncated on write")
}
if got != want {
t.Errorf("want %q, got %q", want, got)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment