Skip to content

Commit d212719

Browse files
authored
Merge pull request #427 from waschik/write-trunc
Write trunc
2 parents fa815b5 + 3467063 commit d212719

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

iolib.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ func ioOpenFile(L *LState) int {
629629
mode = os.O_RDONLY
630630
writable = false
631631
case "w", "wb":
632-
mode = os.O_WRONLY | os.O_CREATE
632+
mode = os.O_WRONLY | os.O_TRUNC | os.O_CREATE
633633
readable = false
634634
case "a", "ab":
635635
mode = os.O_WRONLY | os.O_APPEND | os.O_CREATE

oslib_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package lua
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// correctly gc-ed. There was a bug in gopher lua where local vars were not being gc-ed in all circumstances.
8+
func TestOsWrite(t *testing.T) {
9+
s := `
10+
local function write(filename, content)
11+
local f = assert(io.open(filename, "w"))
12+
f:write(content)
13+
assert(f:close())
14+
end
15+
16+
local filename = os.tmpname()
17+
write(filename, "abc")
18+
write(filename, "d")
19+
local f = assert(io.open(filename, "r"))
20+
local content = f:read("*all"):gsub("%s+", "")
21+
f:close()
22+
os.remove(filename)
23+
local expected = "d"
24+
if content ~= expected then
25+
error(string.format("Invalid content: Expecting \"%s\", got \"%s\"", expected, content))
26+
end
27+
`
28+
L := NewState()
29+
defer L.Close()
30+
if err := L.DoString(s); err != nil {
31+
t.Error(err)
32+
}
33+
}

0 commit comments

Comments
 (0)