forked from kubernetes-sigs/krew
/
tempdir.go
87 lines (75 loc) 路 2.53 KB
/
tempdir.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
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
// Copyright 2019 The Kubernetes Authors.
//
// 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.
package testutil
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
type TempDir struct {
t *testing.T
root string
}
// NewTempDir creates a temporary directory and a cleanup function.
// It is the responsibility of calling code to call cleanup when done.
func NewTempDir(t *testing.T) (tmpDir *TempDir, cleanup func()) {
t.Helper()
root, err := ioutil.TempDir("", "krew-test")
if err != nil {
t.Fatal(err)
}
tmpDir = &TempDir{t: t, root: root}
return tmpDir, func() {
if err := os.RemoveAll(root); err != nil {
t.Logf("warning: failed to remove tempdir %s: %+v", root, err)
}
}
}
// Root returns the root of the temporary directory.
func (td *TempDir) Root() string {
return td.root
}
// Path returns the path to a file in the temp directory.
// The input file is expected to use '/' as directory separator regardless of the host OS.
func (td *TempDir) Path(file string) string {
pathElems := []string{td.root}
pathElems = append(pathElems, strings.Split(file, "/")...)
return filepath.Join(pathElems...)
}
// Write creates a file containing content in the temporary directory.
func (td *TempDir) Write(file string, content []byte) *TempDir {
td.t.Helper()
path := td.Path(file)
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
td.t.Fatalf("cannot create directory %q: %s", filepath.Dir(path), err)
}
if err := ioutil.WriteFile(path, content, os.ModePerm); err != nil {
td.t.Fatalf("cannot write to file %q: %s", path, err)
}
return td
}
// List lists all the files in the subpath of the temp directory.
// The input path is expected to use '/' as directory separator regardless of the host OS.
func (td *TempDir) List(path string) ([]string, error) {
var files []string
err := filepath.Walk(td.Path(path), func(path string, info os.FileInfo, err error) error {
if err == nil && info.Mode().IsRegular() {
files = append(files, filepath.ToSlash(path))
}
return err
})
return files, err
}