-
Notifications
You must be signed in to change notification settings - Fork 947
/
Copy pathtruncate_test.go
61 lines (49 loc) · 1.37 KB
/
truncate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//go:build darwin || (linux && !baremetal && !js && !wasi)
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package os_test
import (
. "os"
"path/filepath"
"runtime"
"testing"
)
func TestTruncate(t *testing.T) {
// Truncate is not supported on Windows or wasi at the moment
if runtime.GOOS == "windows" || runtime.GOOS == "wasip1" || runtime.GOOS == "wasip2" {
t.Logf("skipping test on %s", runtime.GOOS)
return
}
tmpDir := t.TempDir()
file := filepath.Join(tmpDir, "truncate_test")
fd, err := Create(file)
if err != nil {
t.Fatalf("create %q: got %v, want nil", file, err)
}
defer fd.Close()
// truncate up to 0x100
if err := fd.Truncate(0x100); err != nil {
t.Fatalf("truncate %q: got %v, want nil", file, err)
}
// check if size is 0x100
fi, err := Stat(file)
if err != nil {
t.Fatalf("stat %q: got %v, want nil", file, err)
}
if fi.Size() != 0x100 {
t.Fatalf("size of %q is %d; want 0x100", file, fi.Size())
}
// truncate down to 0x80
if err := fd.Truncate(0x80); err != nil {
t.Fatalf("truncate %q: got %v, want nil", file, err)
}
// check if size is 0x80
fi, err = Stat(file)
if err != nil {
t.Fatalf("stat %q: got %v, want nil", file, err)
}
if fi.Size() != 0x80 {
t.Fatalf("size of %q is %d; want 0x80", file, fi.Size())
}
}