Skip to content

Commit 7f3c86c

Browse files
committed
script: add empty command.
Simple command that checks if file or output streams are empty. This provides quality-of-life improvement over having to define a empty file and 'cmp' against that. Signed-off-by: Tom Hadlaw <tom.hadlaw@isovalent.com>
1 parent be36f84 commit 7f3c86c

File tree

2 files changed

+96
-11
lines changed

2 files changed

+96
-11
lines changed

script/cmds.go

Lines changed: 86 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func DefaultCmds() map[string]Cmd {
3333
"cd": Cd(),
3434
"chmod": Chmod(),
3535
"cmp": Cmp(),
36+
"empty": Empty(),
3637
"cmpenv": Cmpenv(),
3738
"cp": Cp(),
3839
"echo": Echo(),
@@ -231,17 +232,10 @@ func doCompare(s *State, env bool, args ...string) error {
231232

232233
name1, name2 := args[0], args[1]
233234
var text1, text2 string
234-
switch name1 {
235-
case "stdout":
236-
text1 = s.Stdout()
237-
case "stderr":
238-
text1 = s.Stderr()
239-
default:
240-
data, err := os.ReadFile(s.Path(name1))
241-
if err != nil {
242-
return err
243-
}
244-
text1 = string(data)
235+
236+
text1, err = getActualFileData(s, name1)
237+
if err != nil {
238+
return err
245239
}
246240

247241
data, err := os.ReadFile(s.Path(name2))
@@ -1232,3 +1226,84 @@ func Break() Cmd {
12321226
},
12331227
)
12341228
}
1229+
1230+
func emptyFlags(fs *pflag.FlagSet) {
1231+
fs.BoolP("quiet", "q", false, "Suppress printing of non-empty output diff")
1232+
fs.BoolP("trim", "t", false, "Trim newline chars")
1233+
}
1234+
1235+
// Empty checks whether contents of a file, or contents of either "stdout" or "stderr"
1236+
// are empty.
1237+
// Returns non-nil error if this assertion fails.
1238+
func Empty() Cmd {
1239+
return Command(
1240+
CmdUsage{
1241+
Args: "file",
1242+
Summary: "check if file is empty",
1243+
Flags: emptyFlags,
1244+
Detail: []string{
1245+
"The command succeeds if the file is empty.",
1246+
"File can be 'stdout' or 'stderr' to compare the stdout or stderr buffer from the most recent command.",
1247+
},
1248+
},
1249+
func(s *State, args ...string) (WaitFunc, error) {
1250+
return nil, doEmpty(s, args...)
1251+
})
1252+
}
1253+
1254+
func doEmpty(s *State, args ...string) error {
1255+
quiet, err := s.Flags.GetBool("quiet")
1256+
if err != nil {
1257+
return err
1258+
}
1259+
trim, err := s.Flags.GetBool("trim")
1260+
if err != nil {
1261+
return err
1262+
}
1263+
if len(args) != 1 {
1264+
return ErrUsage
1265+
}
1266+
1267+
name := args[0]
1268+
text, err := getActualFileData(s, name)
1269+
1270+
if trim {
1271+
text = strings.Trim(text, "\n")
1272+
}
1273+
1274+
if text != "" {
1275+
if s.DoUpdate && s.RetryCount > 0 {
1276+
// Updates requested and we've already retried at least once
1277+
// (and given time for things to settle down).
1278+
// Store the file contents and ignore the mismatch.
1279+
s.FileUpdates[name] = ""
1280+
return nil
1281+
}
1282+
1283+
if !quiet {
1284+
diffText := diff.Diff(name, []byte(text), "<empty>", []byte{})
1285+
s.Logf("%s\n", diffText)
1286+
}
1287+
return fmt.Errorf("%s is not empty", name)
1288+
}
1289+
return nil
1290+
}
1291+
1292+
// getActualFileData is used to read "actual" test data (i.e. such as files
1293+
// created by test script or test stdout.
1294+
func getActualFileData(s *State, name string) (string, error) {
1295+
var text string
1296+
switch name {
1297+
case "stdout":
1298+
text = s.Stdout()
1299+
case "stderr":
1300+
text = s.Stderr()
1301+
default:
1302+
data, err := os.ReadFile(s.Path(name))
1303+
if err != nil {
1304+
return "", err
1305+
}
1306+
text = string(data)
1307+
}
1308+
return text, nil
1309+
}

script/scripttest/testdata/empty.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
empty isempty
2+
3+
# Test stdout facility
4+
empty stdout
5+
6+
# Test trim option
7+
echo
8+
empty --trim stdout
9+
10+
-- isempty --

0 commit comments

Comments
 (0)