File: writefile_test.go

package info (click to toggle)
golang-github-google-renameio 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, experimental, forky, sid, trixie
  • size: 156 kB
  • sloc: makefile: 2
file content (182 lines) | stat: -rw-r--r-- 4,310 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !windows
// +build !windows

package renameio

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
)

func TestWriteFile(t *testing.T) {
	for _, perm := range []os.FileMode{0o755, 0o644, 0o400, 0o765} {
		t.Run(fmt.Sprintf("perm%04o", perm), func(t *testing.T) {
			for _, umask := range []os.FileMode{0o000, 0o011, 0o007, 0o027, 0o077} {
				t.Run(fmt.Sprintf("umask%04o", umask), func(t *testing.T) {
					withUmask(t, umask)

					maskedPerm := perm & ^umask

					filename := filepath.Join(t.TempDir(), "hello.sh")

					wantData := []byte("#!/bin/sh\necho \"Hello World\"\n")
					if err := WriteFile(filename, wantData, perm); err != nil {
						t.Fatal(err)
					}

					gotData, err := ioutil.ReadFile(filename)
					if err != nil {
						t.Fatal(err)
					}
					if !bytes.Equal(gotData, wantData) {
						t.Errorf("got data %v, want data %v", gotData, wantData)
					}

					fi, err := os.Stat(filename)
					if err != nil {
						t.Fatal(err)
					}
					if gotPerm := fi.Mode() & os.ModePerm; gotPerm != maskedPerm {
						t.Errorf("got permissions %04o, want %04o", gotPerm, maskedPerm)
					}
				})
			}
		})
	}
}

func TestWriteFileIgnoreUmask(t *testing.T) {
	withUmask(t, 0o077)

	filename := filepath.Join(t.TempDir(), "file")

	const wantPerm os.FileMode = 0o765

	if err := WriteFile(filename, nil, wantPerm, IgnoreUmask()); err != nil {
		t.Fatal(err)
	}

	fi, err := os.Stat(filename)
	if err != nil {
		t.Fatal(err)
	}
	if gotPerm := fi.Mode() & os.ModePerm; gotPerm != wantPerm {
		t.Errorf("got permissions %04o, want %04o", gotPerm, wantPerm)
	}
}

func TestWriteFileEquivalence(t *testing.T) {
	type writeFunc func(string, []byte, os.FileMode, ...Option) error
	type test struct {
		name   string
		fn     writeFunc
		perm   os.FileMode
		umask  os.FileMode
		exists bool
	}

	var tests []test

	for _, wf := range []struct {
		name string
		fn   writeFunc
	}{
		{
			name: "WriteFile",
			fn:   WriteFile,
		},
		{
			name: "ioutil",
			fn: func(filename string, data []byte, perm os.FileMode, opts ...Option) error {
				return ioutil.WriteFile(filename, data, perm)
			},
		},
	} {
		for _, perm := range []os.FileMode{0o755, 0o644, 0o400, 0o765} {
			for _, umask := range []os.FileMode{0o000, 0o011, 0o007, 0o027, 0o077} {
				for _, exists := range []bool{false, true} {
					name := fmt.Sprintf("%s/perm%04o/umask%04o", wf.name, perm, umask)
					if exists {
						name += "/exists"
					}

					tests = append(tests, test{
						name:   name,
						fn:     wf.fn,
						perm:   perm,
						umask:  umask,
						exists: exists,
					})
				}
			}
		}
	}

	const existingPerm os.FileMode = 0o654

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			withUmask(t, tc.umask)

			maskedPerm := tc.perm & ^tc.umask

			filename := filepath.Join(t.TempDir(), "test.txt")

			if tc.exists {
				// Create file in preparation for replacement
				fh, err := os.Create(filename)
				if err != nil {
					t.Errorf("Create(%q) failed: %v", filename, err)
				}

				if err := fh.Chmod(existingPerm); err != nil {
					t.Errorf("Chmod() failed: %v", err)
				}

				fh.Close()

				maskedPerm = existingPerm
			}

			wantData := []byte("content\n")

			if err := tc.fn(filename, wantData, tc.perm); err != nil {
				t.Fatal(err)
			}

			gotData, err := ioutil.ReadFile(filename)
			if err != nil {
				t.Fatal(err)
			}
			if !bytes.Equal(gotData, wantData) {
				t.Errorf("got data %v, want data %v", gotData, wantData)
			}

			fi, err := os.Stat(filename)
			if err != nil {
				t.Fatal(err)
			}
			if gotPerm := fi.Mode() & os.ModePerm; gotPerm != maskedPerm {
				t.Errorf("got permissions %04o, want %04o", gotPerm, maskedPerm)
			}
		})
	}
}