Skip to content

Commit 56c9232

Browse files
committed
Fixed bug when using write command
Added more tests to avoid bugs ;)
1 parent c122b9a commit 56c9232

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

precheckin.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
gofmt -w .
44
golint
55
go test
6+
go build
67

78
# acceptance test
8-
go build
9-
X=$(./yaml r sample.yaml b.c)
9+
X=$(./yaml w sample.yaml b.c 3 | ./yaml r - b.c)
1010

11-
if [ $X != 2 ]
11+
if [ $X != 3 ]
1212
then
1313
echo "Failed acceptance test: expected 2 but was $X"
1414
exit 1

yaml.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,40 @@ Outputs to STDOUT unless the inplace flag is used, in which case the file is upd
5353
}
5454

5555
func readProperty(cmd *cobra.Command, args []string) {
56+
printYaml(read(args))
57+
}
58+
59+
func read(args []string) interface{} {
5660
var parsedData map[interface{}]interface{}
5761

5862
readYaml(args[0], &parsedData)
5963

6064
if len(args) == 1 {
61-
printYaml(parsedData)
62-
os.Exit(0)
65+
return parsedData
6366
}
6467

6568
var paths = parsePath(args[1])
6669

67-
printYaml(readMap(parsedData, paths[0], paths[1:len(paths)]))
70+
return readMap(parsedData, paths[0], paths[1:len(paths)])
6871
}
6972

7073
func writeProperty(cmd *cobra.Command, args []string) {
74+
updatedData := updateYaml(args)
75+
if writeInplace {
76+
ioutil.WriteFile(args[0], []byte(yamlToString(updatedData)), 0644)
77+
} else {
78+
printYaml(updatedData)
79+
}
80+
}
81+
82+
func updateYaml(args []string) interface{} {
7183
var writeCommands map[string]interface{}
7284
if writeScript != "" {
7385
readYaml(writeScript, &writeCommands)
7486
} else if len(args) < 3 {
7587
die("Must provide <filename> <path_to_update> <value>")
7688
} else {
89+
writeCommands = make(map[string]interface{})
7790
writeCommands[args[1]] = parseValue(args[2])
7891
}
7992

@@ -84,13 +97,9 @@ func writeProperty(cmd *cobra.Command, args []string) {
8497
var paths = parsePath(path)
8598
write(parsedData, paths[0], paths[1:len(paths)], value)
8699
}
87-
88-
if writeInplace {
89-
ioutil.WriteFile(args[0], []byte(yamlToString(parsedData)), 0644)
90-
} else {
91-
printYaml(parsedData)
92-
}
100+
return parsedData
93101
}
102+
94103
func parseValue(argument string) interface{} {
95104
var value, err interface{}
96105
var inQuotes = argument[0] == '"'

yaml_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,17 @@ func TestParseValue(t *testing.T) {
2020
assertResultWithContext(t, tt.expectedResult, parseValue(tt.argument), tt.testDescription)
2121
}
2222
}
23+
24+
func TestRead(t *testing.T) {
25+
result := read([]string{"sample.yaml", "b.c"})
26+
assertResult(t, 2, result)
27+
}
28+
29+
func TestUpdateYaml(t *testing.T) {
30+
updateYaml([]string{"sample.yaml", "b.c", "3"})
31+
}
32+
33+
func TestUpdateYaml_WithScript(t *testing.T) {
34+
writeScript = "instruction_sample.yaml"
35+
updateYaml([]string{"sample.yaml"})
36+
}

0 commit comments

Comments
 (0)