-
Notifications
You must be signed in to change notification settings - Fork 0
/
filevar_test.go
197 lines (159 loc) · 4.83 KB
/
filevar_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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENSE file for details.
package cmd_test
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/juju/gnuflag"
gitjujutesting "github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils/v3"
gc "gopkg.in/check.v1"
"github.com/juju/cmd/v3"
"github.com/juju/cmd/v3/cmdtesting"
)
type FileVarSuite struct {
gitjujutesting.FakeHomeSuite
ctx *cmd.Context
ValidPath string
InvalidPath string // invalid path refers to a file which is not readable
}
var _ = gc.Suite(&FileVarSuite{})
func (s *FileVarSuite) SetUpTest(c *gc.C) {
s.FakeHomeSuite.SetUpTest(c)
s.ctx = cmdtesting.Context(c)
s.ValidPath = s.ctx.AbsPath("valid.yaml")
s.InvalidPath = s.ctx.AbsPath("invalid.yaml")
f, err := os.Create(s.ValidPath)
c.Assert(err, gc.IsNil)
f.Close()
f, err = os.Create(s.InvalidPath)
c.Assert(err, gc.IsNil)
f.Close()
err = os.Chmod(s.InvalidPath, 0) // make unreadable
c.Assert(err, gc.IsNil)
}
func (s *FileVarSuite) TestSetStdin(c *gc.C) {
var config cmd.FileVar
c.Assert(config.Path, gc.Equals, "")
c.Assert(config.StdinMarkers, jc.DeepEquals, []string{})
config.SetStdin()
c.Assert(config.Path, gc.Equals, "")
c.Assert(config.StdinMarkers, jc.DeepEquals, []string{"-"})
config.SetStdin("<>", "@")
c.Assert(config.Path, gc.Equals, "")
c.Assert(config.StdinMarkers, jc.DeepEquals, []string{"<>", "@"})
}
func (s *FileVarSuite) TestIsStdin(c *gc.C) {
var config cmd.FileVar
c.Check(config.IsStdin(), jc.IsFalse)
config.StdinMarkers = []string{"-"}
c.Check(config.IsStdin(), jc.IsFalse)
config.Path = "spam"
c.Check(config.IsStdin(), jc.IsFalse)
config.Path = "-"
c.Check(config.IsStdin(), jc.IsTrue)
config.StdinMarkers = nil
c.Check(config.IsStdin(), jc.IsFalse)
config.StdinMarkers = []string{"<>", "@"}
c.Check(config.IsStdin(), jc.IsFalse)
config.Path = "<>"
c.Check(config.IsStdin(), jc.IsTrue)
config.Path = "@"
c.Check(config.IsStdin(), jc.IsTrue)
}
func (FileVarSuite) checkOpen(c *gc.C, file io.ReadCloser, expected string) {
defer file.Close()
data, err := ioutil.ReadAll(file)
c.Assert(err, jc.ErrorIsNil)
c.Check(string(data), gc.Equals, expected)
}
func (s *FileVarSuite) TestOpenTilde(c *gc.C) {
path := filepath.Join(utils.Home(), "config.yaml")
err := ioutil.WriteFile(path, []byte("abc"), 0644)
c.Assert(err, gc.IsNil)
var config cmd.FileVar
config.Set("~/config.yaml")
file, err := config.Open(s.ctx)
c.Assert(err, gc.IsNil)
s.checkOpen(c, file, "abc")
}
func (s *FileVarSuite) TestOpenStdin(c *gc.C) {
s.ctx.Stdin = bytes.NewBufferString("abc")
var config cmd.FileVar
config.SetStdin()
config.Set("-")
file, err := config.Open(s.ctx)
c.Assert(err, gc.IsNil)
s.checkOpen(c, file, "abc")
}
func (s *FileVarSuite) TestOpenNotStdin(c *gc.C) {
var config cmd.FileVar
config.Set("-")
_, err := config.Open(s.ctx)
c.Check(err, jc.Satisfies, os.IsNotExist)
}
func (s *FileVarSuite) TestOpenValid(c *gc.C) {
fs, config := fs()
err := fs.Parse(false, []string{"--config", s.ValidPath})
c.Assert(err, gc.IsNil)
c.Assert(config.Path, gc.Equals, s.ValidPath)
_, err = config.Open(s.ctx)
c.Assert(err, gc.IsNil)
}
func (s *FileVarSuite) TestOpenInvalid(c *gc.C) {
fs, config := fs()
err := fs.Parse(false, []string{"--config", s.InvalidPath})
c.Assert(config.Path, gc.Equals, s.InvalidPath)
_, err = config.Open(s.ctx)
c.Assert(err, gc.ErrorMatches, "*permission denied")
}
func (s *FileVarSuite) TestReadTilde(c *gc.C) {
path := filepath.Join(utils.Home(), "config.yaml")
err := ioutil.WriteFile(path, []byte("abc"), 0644)
c.Assert(err, gc.IsNil)
var config cmd.FileVar
config.Set("~/config.yaml")
file, err := config.Read(s.ctx)
c.Assert(err, gc.IsNil)
c.Assert(string(file), gc.Equals, "abc")
}
func (s *FileVarSuite) TestReadStdin(c *gc.C) {
s.ctx.Stdin = bytes.NewBufferString("abc")
var config cmd.FileVar
config.SetStdin()
config.Set("-")
file, err := config.Read(s.ctx)
c.Assert(err, gc.IsNil)
c.Assert(string(file), gc.Equals, "abc")
}
func (s *FileVarSuite) TestReadNotStdin(c *gc.C) {
var config cmd.FileVar
config.Set("-")
_, err := config.Read(s.ctx)
c.Check(err, jc.Satisfies, os.IsNotExist)
}
func (s *FileVarSuite) TestReadValid(c *gc.C) {
fs, config := fs()
err := fs.Parse(false, []string{"--config", s.ValidPath})
c.Assert(err, gc.IsNil)
c.Assert(config.Path, gc.Equals, s.ValidPath)
_, err = config.Read(s.ctx)
c.Assert(err, gc.IsNil)
}
func (s *FileVarSuite) TestReadInvalid(c *gc.C) {
fs, config := fs()
err := fs.Parse(false, []string{"--config", s.InvalidPath})
c.Assert(config.Path, gc.Equals, s.InvalidPath)
_, err = config.Read(s.ctx)
c.Assert(err, gc.ErrorMatches, "*permission denied")
}
func fs() (*gnuflag.FlagSet, *cmd.FileVar) {
var config cmd.FileVar
fs := cmdtesting.NewFlagSet()
fs.Var(&config, "config", "the config")
return fs, &config
}