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