From 573471137b3255fe2c9995a11451f7ac99b77b1a Mon Sep 17 00:00:00 2001 From: "Hristos N. Triantafillou" Date: Sat, 29 Feb 2020 11:34:13 -0600 Subject: [PATCH] Don't try to use bad data from YAML, print a warning when needed Bad data from YAML is ignored, and if no usable data is found then a warning is printed to give the user a clue about what's wrong. I also broke some more code out of the cli Action definition to make testing easier. And finally, I added the 'reallybad.yml' file which has no usable data, for testing. --- obpmg.go | 44 +++++++++++++++++++++++++------------------- obpmg_test.go | 25 +++++++++++++++++++++++++ reallybad.yml | 12 ++++++++++++ 3 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 reallybad.yml diff --git a/obpmg.go b/obpmg.go index b38377f..ced6f3f 100644 --- a/obpmg.go +++ b/obpmg.go @@ -50,10 +50,6 @@ type yamlData struct { Exe string `yaml:"exe"` } -func cleanYamlData() { - // -} - func getYamlData(filePath string, nosort bool) ([]yamlData, interface{}) { file, err := ioutil.ReadFile(filePath) if err != nil { @@ -75,6 +71,30 @@ func getYamlData(filePath string, nosort bool) ([]yamlData, interface{}) { return y, err } +func yamlDataToX(x *xmlOpenboxPipeMenu, y []yamlData) *xmlOpenboxPipeMenu { + for _, data := range y { + // Don't use bad or empty data + if (data != yamlData{}) { + x.Items = append(x.Items, xmlItem{ + xml.Name{}, + xmlAction{ + xml.Name{}, + "Execute", + data.Exe, + }, + data.Label, + }, + ) + } + } + + if len(x.Items) == 0 { + log.Print("No usable data found in the given YAML file!") + } + + return x +} + func cliApp() *cli.App { var nosort bool var yamlFile string @@ -108,26 +128,12 @@ func cliApp() *cli.App { yamlFile := c.String("yaml-file") - //TODO: validate the yaml data y, err := getYamlData(yamlFile, nosort) if err != nil { log.Fatal(err) } - x := &xmlOpenboxPipeMenu{} - - for _, data := range y { - x.Items = append(x.Items, xmlItem{ - xml.Name{}, - xmlAction{ - xml.Name{}, - "Execute", - data.Exe, - }, - data.Label, - }, - ) - } + x := yamlDataToX(&xmlOpenboxPipeMenu{}, y) out := toXml(x) os.Stdout.Write(out) diff --git a/obpmg_test.go b/obpmg_test.go index 08a357c..2c93f1c 100644 --- a/obpmg_test.go +++ b/obpmg_test.go @@ -23,6 +23,7 @@ import ( func TestGetYamlData(t *testing.T) { y, _ := getYamlData("sample.yml", false) + x := yamlDataToX(&xmlOpenboxPipeMenu{}, y) if y[0].Label != "FTL: Faster Than Light" { t.Errorf("Expected: %s, Got: %s", "FTL: Faster Than Light", y[0].Label) @@ -35,6 +36,30 @@ func TestGetYamlData(t *testing.T) { if y[2].Label != "OpenMW" { t.Errorf("Expected: %s, Got: %s", "OpenMW", y[2].Label) } + + if len(x.Items) != 3 { + t.Errorf("Expected: %d, Got: %d", 3, len(x.Items)) + } +} + +func TestGetYamlDataBad(t *testing.T) { + y, _ := getYamlData("bad.yml", false) + x := yamlDataToX(&xmlOpenboxPipeMenu{}, y) + + // The bad item should be omitted + if len(x.Items) != 3 { + t.Errorf("Expected: %d, Got: %d", 3, len(x.Items)) + } +} + +func TestGetYamlDataReallyBad(t *testing.T) { + y, _ := getYamlData("reallybad.yml", false) + x := yamlDataToX(&xmlOpenboxPipeMenu{}, y) + + // No good data was given + if len(x.Items) != 0 { + t.Errorf("Expected: %d, Got: %d", 0, len(x.Items)) + } } func TestToXml(t *testing.T) { diff --git a/reallybad.yml b/reallybad.yml new file mode 100644 index 0000000..bee2448 --- /dev/null +++ b/reallybad.yml @@ -0,0 +1,12 @@ +--- +- bad: data + nothing: here + +- bad: data + nothing: here + +- bad: data + nothing: here + +- bad: data + nothing: here -- 2.45.2