Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
First ParseConfig implementation, shaky tests
  • Loading branch information
mrnugget committed Mar 26, 2013
1 parent 1d3bd77 commit 9a8836d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
28 changes: 27 additions & 1 deletion config.go
@@ -1,11 +1,37 @@
package main

import (
"encoding/json"
"io/ioutil"
)

var f interface{}

type Rule struct {
Path string
Run string
}

func ParseConfig(path string) (rules []*Rule, err error) {
rules = make([]*Rule, 0)
return

c, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}

err = json.Unmarshal(c, &f)
if err != nil {
return nil, err
}

config := f.(map[string]interface{})
for path, v := range config {
attributes := v.(map[string]interface{})
run := attributes["run"].(string)

rules = append(rules, &Rule{path, run})
}

return rules, nil
}
32 changes: 31 additions & 1 deletion config_test.go
Expand Up @@ -4,9 +4,39 @@ import (
"testing"
)

var parseTests = []struct {
index int
expectedPath string
expectedRun string
}{
{
0,
"/tmp/foo",
"/usr/local/bar/foobar",
},
{
1,
"/tmp/bar",
"/usr/local/bar/barfoo",
},
}

func TestParseConfig(t *testing.T) {
_, err := ParseConfig(fixtures + "/example_config.json")
rules, err := ParseConfig(fixtures + "/example_config.json")
if err != nil {
t.Fatal(err)
}
if len(rules) != 2 {
t.Fatal("Did not include all rules")
}

for _, pt := range parseTests {
actualPath := rules[pt.index].Path
actualRun := rules[pt.index].Run

if actualPath != pt.expectedPath && actualRun != pt.expectedRun {
t.Errorf("ACTUALPATH %v - EXPECTEDPATH %v, ACTUALRUN %v - EXPECTEDRUN %v", actualPath, pt.expectedPath, actualRun, pt.expectedRun)
continue
}
}
}
2 changes: 1 addition & 1 deletion fixtures/example_config.json
Expand Up @@ -3,6 +3,6 @@
"run": "/usr/local/bar/foobar"
},
"/tmp/bar": {
"run": "/usr/local/bar/foobar"
"run": "/usr/local/bar/barfoo"
}
}

0 comments on commit 9a8836d

Please sign in to comment.