diff options
author | Cameron Moore <moorereason@gmail.com> | 2016-12-26 15:23:20 -0600 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-03-11 17:52:25 +0100 |
commit | f039e3be9e4a11808508c8cd3043b340deea040f (patch) | |
tree | df3e709fc6321fd2a3e1d23efa2c3512d8ed4a94 /commands | |
parent | ddc8cc0082965143a650052a9aa538bac9133481 (diff) |
parser: Refactor frontmatter parser and add tests
Lots of cleanups here:
- Refactor InterfaceToConfig and InterfaceToFrontMatter to use io.Writer.
- Simplify InterfaceToFrontMatter by wrapping InterfaceToConfig.
- Export FrontmatterType since we return it in DetectFrontMatter.
- Refactor removeTOMLIdentifier to avoid blindly replacing "+++".
- Update HandleJSONMetaData to return an empty map on nil input.
- Updates vendored goorgeous package and test for org-mode frontmatter.
- Add tests and godoc comments.
Coverage for parser package increased from 45.2% to 85.2%.
Diffstat (limited to 'commands')
-rw-r--r-- | commands/import_jekyll.go | 10 | ||||
-rw-r--r-- | commands/new.go | 10 | ||||
-rw-r--r-- | commands/undraft_test.go | 12 |
3 files changed, 12 insertions, 20 deletions
diff --git a/commands/import_jekyll.go b/commands/import_jekyll.go index 151fffa8a..c33b68f2e 100644 --- a/commands/import_jekyll.go +++ b/commands/import_jekyll.go @@ -251,17 +251,13 @@ func createConfigFromJekyll(fs afero.Fs, inpath string, kind string, jekyllConfi } kind = parser.FormatSanitize(kind) - by, err := parser.InterfaceToConfig(in, parser.FormatToLeadRune(kind)) + var buf bytes.Buffer + err = parser.InterfaceToConfig(in, parser.FormatToLeadRune(kind), &buf) if err != nil { return err } - err = helpers.WriteToDisk(filepath.Join(inpath, "config."+kind), bytes.NewReader(by), fs) - if err != nil { - return - } - - return nil + return helpers.WriteToDisk(filepath.Join(inpath, "config."+kind), &buf, fs) } func copyFile(source string, dest string) error { diff --git a/commands/new.go b/commands/new.go index fbecb6854..7bab13e97 100644 --- a/commands/new.go +++ b/commands/new.go @@ -356,15 +356,11 @@ func createConfig(fs *hugofs.Fs, inpath string, kind string) (err error) { } kind = parser.FormatSanitize(kind) - by, err := parser.InterfaceToConfig(in, parser.FormatToLeadRune(kind)) + var buf bytes.Buffer + err = parser.InterfaceToConfig(in, parser.FormatToLeadRune(kind), &buf) if err != nil { return err } - err = helpers.WriteToDisk(filepath.Join(inpath, "config."+kind), bytes.NewReader(by), fs.Source) - if err != nil { - return - } - - return nil + return helpers.WriteToDisk(filepath.Join(inpath, "config."+kind), &buf, fs.Source) } diff --git a/commands/undraft_test.go b/commands/undraft_test.go index 45b785bb0..6ed172205 100644 --- a/commands/undraft_test.go +++ b/commands/undraft_test.go @@ -46,17 +46,17 @@ func TestUndraftContent(t *testing.T) { {yamlDraftFM, ""}, } - for _, test := range tests { + for i, test := range tests { r := bytes.NewReader([]byte(test.fm)) p, _ := parser.ReadFrom(r) res, err := undraftContent(p) if test.expectedErr != "" { if err == nil { - t.Error("Expected error, got none") + t.Error("[%d] Expected error, got none", i) continue } if err.Error() != test.expectedErr { - t.Errorf("Expected %q, got %q", test.expectedErr, err) + t.Errorf("[%d] Expected %q, got %q", i, test.expectedErr, err) continue } } else { @@ -64,19 +64,19 @@ func TestUndraftContent(t *testing.T) { p, _ = parser.ReadFrom(r) meta, err := p.Metadata() if err != nil { - t.Errorf("unexpected error %q", err) + t.Errorf("[%d] unexpected error %q", i, err) continue } for k, v := range meta.(map[string]interface{}) { if k == "draft" { if v.(bool) { - t.Errorf("Expected %q to be \"false\", got \"true\"", k) + t.Errorf("[%d] Expected %q to be \"false\", got \"true\"", i, k) continue } } if k == "date" { if !strings.HasPrefix(v.(string), time.Now().Format("2006-01-02")) { - t.Errorf("Expected %v to start with %v", v.(string), time.Now().Format("2006-01-02")) + t.Errorf("[%d] Expected %v to start with %v", i, v.(string), time.Now().Format("2006-01-02")) } } } |