Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
[#22] Add Dash comment style
Browse files Browse the repository at this point in the history
  • Loading branch information
dialaya authored and Florent Biville committed Feb 1, 2019
1 parent ece3139 commit c810dac
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
17 changes: 17 additions & 0 deletions core/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ func (SlashSlash) GetClosingString() string {
return ""
}

type Hash struct{}

func (Hash) GetName() string {
return "Hash"
}
func (Hash) GetOpeningString() string {
return ""
}
func (Hash) GetString() string {
return "# "
}
func (Hash) GetClosingString() string {
return ""
}

func ParseCommentStyle(str string) CommentStyle {
styles := supportedStyles()
keys := extractKeys(styles)
Expand Down Expand Up @@ -142,6 +157,8 @@ func supportedStyles() map[string]CommentStyle {
return map[string]CommentStyle{
"SlashStar": SlashStar{},
"SlashSlash": SlashSlash{},
"Hash": Hash{},

}
}

Expand Down
19 changes: 19 additions & 0 deletions core/comment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core_test

import (
. "github.com/fbiville/headache/core"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Comment", func() {

It("matches Dash comment style", func() {
style := ParseCommentStyle("Hash")

Expect(style.GetName()).To(Equal("Hash"))
Expect(style.GetClosingString()).To(Equal(""))
Expect(style.GetOpeningString()).To(Equal(""))
Expect(style.GetString()).To(Equal("# "))
})
})
2 changes: 1 addition & 1 deletion core/configuration_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (cl *ConfigurationLoader) validateConfiguration(configFile *string) error {
}

func loadSchema() *jsonsch.Schema {
schema, err := jsonsch.NewSchema(jsonsch.NewReferenceLoader("https://fbiville.github.io/headache/v1.0.0-M01/schema.json"))
schema, err := jsonsch.NewSchema(jsonsch.NewReferenceLoader("https://fbiville.github.io/headache/schema.json"))
if err != nil {
log.Printf("headache configuration warning: cannot load schema, skipping configuration validation. See reason below:\n\t%v\n", err)
return nil
Expand Down
27 changes: 26 additions & 1 deletion core/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,28 @@ var _ = Describe("Configuration parser", func() {
Expect(onlyPaths(changeSet.Files)).To(Equal([]FileChange{{Path: "hello-world.go"}}))
})

It("pre-computes the header contents with a different comment style", func() {
It("pre-computes the final configuration with hash comment style", func() {
configuration := &core.Configuration{
HeaderFile: "some-header",
CommentStyle: "Hash",
Includes: includes,
Excludes: excludes,
TemplateData: data,
}
tracker.On("RetrieveVersionedTemplate", configuration).
Return(unchangedHeaderContents("Copyright {{.Year}} {{.Owner}}\n\nSome fictional license", data, revision), nil)
versioningClient.On("GetChanges", revision).Return(initialChanges, nil)
pathMatcher.On("MatchFiles", initialChanges, includes, excludes, fileSystem).Return(resultingChanges)
versioningClient.On("AddMetadata", resultingChanges, clock).Return(resultingChanges, nil)

changeSet, err := core.ParseConfiguration(configuration, systemConfiguration, tracker, pathMatcher)

Expect(err).To(BeNil())
Expect(changeSet.HeaderContents).To(Equal("# Copyright {{.Year}} ACME Labs\n#\n# Some fictional license"))
Expect(onlyPaths(changeSet.Files)).To(Equal([]FileChange{{Path: "hello-world.go"}}))
})

It("pre-computes the header contents with SlashStar comment style", func() {
configuration := &core.Configuration{
HeaderFile: "some-header",
CommentStyle: "SlashStar",
Expand Down Expand Up @@ -151,11 +172,15 @@ var _ = Describe("Configuration parser", func() {
Expect(regex.MatchString(changeSet.HeaderContents)).To(BeTrue(), "Regex should match contents")
Expect(regex.MatchString("// Copyright 2018 ACME Labs")).To(BeTrue(),
"Regex should match contents with different comment style")
Expect(regex.MatchString("# Copyright 2018 ACME Labs")).To(BeTrue(),
"Regex should match contents with different comment style")
Expect(regex.MatchString(`/*
* Copyright 2018-2042 ACME World corporation
*/`)).To(BeTrue(), "Regex should match contents with different data")
Expect(regex.MatchString("// Copyright 2009-2012 ACME!")).To(BeTrue(),
"Regex should match contents with different data and comment style")
Expect(regex.MatchString("# Copyright 2009-2012 ACME!")).To(BeTrue(),
"Regex should match contents with different data and comment style")
})

It("computes the header regex based on previous configuration", func() {
Expand Down
13 changes: 11 additions & 2 deletions core/configuration_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,18 @@ var _ = Describe("Configuration validator", func() {
Expect(validationError).To(BeNil())
})

It("accepts valid configuration", func() {
It("accepts valid configuration with SlashSlash comment style", func() {
fileReader.On("Open", "docs.json").
Return(inMemoryFile(`{"headerFile": "some-file.txt", "style": "SlashStar", "includes": ["**/*.go"], "data": {"FooBar": true}}`), nil)
Return(inMemoryFile(`{"headerFile": "some-file.txt", "style": "SlashSlash", "includes": ["**/*.go"], "data": {"FooBar": true}}`), nil)

validationError := validator.Validate("file://docs.json")

Expect(validationError).To(BeNil())
})

It("accepts valid configuration with Hash comment style", func() {
fileReader.On("Open", "docs.json").
Return(inMemoryFile(`{"headerFile": "some-file.txt", "style": "Hash", "includes": ["**/*.go"]}`), nil)

validationError := validator.Validate("file://docs.json")

Expand Down
3 changes: 2 additions & 1 deletion docs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"type": "string",
"enum": [
"SlashStar",
"SlashSlash"
"SlashSlash",
"Hash"
]
},
"includes": {
Expand Down

0 comments on commit c810dac

Please sign in to comment.