Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
script
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
bitfield
script
Commits
24bffd5c
Unverified
Commit
24bffd5c
authored
3 years ago
by
John Arundel
Committed by
GitHub
3 years ago
Browse files
Options
Downloads
Plain Diff
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
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.circleci/config.yml
+1
-1
1 addition, 1 deletion
.circleci/config.yml
README.md
+1
-1
1 addition, 1 deletion
README.md
sinks.go
+5
-4
5 additions, 4 deletions
sinks.go
sinks_test.go
+47
-19
47 additions, 19 deletions
sinks_test.go
with
54 additions
and
25 deletions
.circleci/config.yml
+
1
−
1
View file @
24bffd5c
...
...
@@ -2,7 +2,7 @@ version: 2
jobs
:
test
:
docker
:
-
image
:
circleci/golang:1.1
2
-
image
:
circleci/golang:1.1
7
steps
:
-
checkout
-
run
:
go test ./...
...
...
This diff is collapsed.
Click to expand it.
README.md
+
1
−
1
View file @
24bffd5c
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
sinks.go
+
5
−
4
View file @
24bffd5c
...
...
@@ -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
)
{
...
...
This diff is collapsed.
Click to expand it.
sinks_test.go
+
47
−
19
View file @
24bffd5c
...
...
@@ -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
TestWriteFile
New
(
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
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment