File: reader_test.go

package info (click to toggle)
golang-github-u-root-uio 0.0~git20240224.d2acac8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 276 kB
  • sloc: sh: 25; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,579 bytes parent folder | download | duplicates (2)
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
62
63
64
// Copyright 2021 the u-root 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 uio

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func readAndCheck(t *testing.T, want, tmpfileP string) {
	t.Helper()
	r := strings.NewReader(want)
	if err := ReadIntoFile(r, tmpfileP); err != nil {
		t.Errorf("ReadIntoFile(%v, %s) = %v, want no error", r, tmpfileP, err)
	}

	got, err := os.ReadFile(tmpfileP)
	if err != nil {
		t.Fatalf("os.ReadFile(%s) = %v, want no error", tmpfileP, err)
	}
	if want != string(got) {
		t.Errorf("got: %v, want %s", string(got), want)
	}
}

func TestReadIntoFile(t *testing.T) {
	want := "I am the wanted"

	dir := t.TempDir()

	// Write to a file already exist.
	p := filepath.Join(dir, "uio-out")
	// Expect net effect to be creating a new empty file: "uio-out".
	f, err := os.OpenFile(p, os.O_RDONLY|os.O_CREATE|os.O_TRUNC, 0o755)
	if err != nil {
		t.Fatal(err)
	}
	if err := f.Close(); err != nil {
		t.Fatal(err)
	}
	readAndCheck(t, want, f.Name())

	// Write to a file that does not exist.
	p = filepath.Join(dir, "uio-out-not-existing")
	readAndCheck(t, want, p)

	// Write to an existing file that has pre-existing content.
	p = filepath.Join(dir, "uio-out-prexist-content")
	f, err = os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755)
	if err != nil {
		t.Fatal(err)
	}
	if _, err := f.Write([]byte("temporary file's content")); err != nil {
		t.Fatal(err)
	}
	if err := f.Close(); err != nil {
		t.Fatal(err)
	}
	readAndCheck(t, want, p)
}