Fixed bug when using write command

Added more tests to avoid bugs ;)
This commit is contained in:
Mike Farah 2015-10-09 21:24:42 +11:00
parent c122b9a843
commit 56c923228d
3 changed files with 35 additions and 12 deletions

View File

@ -3,12 +3,12 @@
gofmt -w . gofmt -w .
golint golint
go test go test
go build
# acceptance test # acceptance test
go build X=$(./yaml w sample.yaml b.c 3 | ./yaml r - b.c)
X=$(./yaml r sample.yaml b.c)
if [ $X != 2 ] if [ $X != 3 ]
then then
echo "Failed acceptance test: expected 2 but was $X" echo "Failed acceptance test: expected 2 but was $X"
exit 1 exit 1

27
yaml.go
View File

@ -53,27 +53,40 @@ Outputs to STDOUT unless the inplace flag is used, in which case the file is upd
} }
func readProperty(cmd *cobra.Command, args []string) { func readProperty(cmd *cobra.Command, args []string) {
printYaml(read(args))
}
func read(args []string) interface{} {
var parsedData map[interface{}]interface{} var parsedData map[interface{}]interface{}
readYaml(args[0], &parsedData) readYaml(args[0], &parsedData)
if len(args) == 1 { if len(args) == 1 {
printYaml(parsedData) return parsedData
os.Exit(0)
} }
var paths = parsePath(args[1]) var paths = parsePath(args[1])
printYaml(readMap(parsedData, paths[0], paths[1:len(paths)])) return readMap(parsedData, paths[0], paths[1:len(paths)])
} }
func writeProperty(cmd *cobra.Command, args []string) { func writeProperty(cmd *cobra.Command, args []string) {
updatedData := updateYaml(args)
if writeInplace {
ioutil.WriteFile(args[0], []byte(yamlToString(updatedData)), 0644)
} else {
printYaml(updatedData)
}
}
func updateYaml(args []string) interface{} {
var writeCommands map[string]interface{} var writeCommands map[string]interface{}
if writeScript != "" { if writeScript != "" {
readYaml(writeScript, &writeCommands) readYaml(writeScript, &writeCommands)
} else if len(args) < 3 { } else if len(args) < 3 {
die("Must provide <filename> <path_to_update> <value>") die("Must provide <filename> <path_to_update> <value>")
} else { } else {
writeCommands = make(map[string]interface{})
writeCommands[args[1]] = parseValue(args[2]) writeCommands[args[1]] = parseValue(args[2])
} }
@ -84,13 +97,9 @@ func writeProperty(cmd *cobra.Command, args []string) {
var paths = parsePath(path) var paths = parsePath(path)
write(parsedData, paths[0], paths[1:len(paths)], value) write(parsedData, paths[0], paths[1:len(paths)], value)
} }
return parsedData
if writeInplace {
ioutil.WriteFile(args[0], []byte(yamlToString(parsedData)), 0644)
} else {
printYaml(parsedData)
}
} }
func parseValue(argument string) interface{} { func parseValue(argument string) interface{} {
var value, err interface{} var value, err interface{}
var inQuotes = argument[0] == '"' var inQuotes = argument[0] == '"'

View File

@ -20,3 +20,17 @@ func TestParseValue(t *testing.T) {
assertResultWithContext(t, tt.expectedResult, parseValue(tt.argument), tt.testDescription) assertResultWithContext(t, tt.expectedResult, parseValue(tt.argument), tt.testDescription)
} }
} }
func TestRead(t *testing.T) {
result := read([]string{"sample.yaml", "b.c"})
assertResult(t, 2, result)
}
func TestUpdateYaml(t *testing.T) {
updateYaml([]string{"sample.yaml", "b.c", "3"})
}
func TestUpdateYaml_WithScript(t *testing.T) {
writeScript = "instruction_sample.yaml"
updateYaml([]string{"sample.yaml"})
}