From c810dac6828320e499e1b6f2adf6c9a0f543856d Mon Sep 17 00:00:00 2001 From: bakary djiba Date: Tue, 29 Jan 2019 22:11:45 +0100 Subject: [PATCH] [#22] Add Dash comment style --- core/comment.go | 17 +++++++++++++++++ core/comment_test.go | 19 +++++++++++++++++++ core/configuration_loader.go | 2 +- core/configuration_test.go | 27 ++++++++++++++++++++++++++- core/configuration_validator_test.go | 13 +++++++++++-- docs/schema.json | 3 ++- 6 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 core/comment_test.go diff --git a/core/comment.go b/core/comment.go index 51b5892..662cb53 100644 --- a/core/comment.go +++ b/core/comment.go @@ -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) @@ -142,6 +157,8 @@ func supportedStyles() map[string]CommentStyle { return map[string]CommentStyle{ "SlashStar": SlashStar{}, "SlashSlash": SlashSlash{}, + "Hash": Hash{}, + } } diff --git a/core/comment_test.go b/core/comment_test.go new file mode 100644 index 0000000..eea1c02 --- /dev/null +++ b/core/comment_test.go @@ -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("# ")) + }) +}) diff --git a/core/configuration_loader.go b/core/configuration_loader.go index 32eb9f4..875d683 100644 --- a/core/configuration_loader.go +++ b/core/configuration_loader.go @@ -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 diff --git a/core/configuration_test.go b/core/configuration_test.go index 6d35a7f..ce35aca 100644 --- a/core/configuration_test.go +++ b/core/configuration_test.go @@ -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", @@ -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() { diff --git a/core/configuration_validator_test.go b/core/configuration_validator_test.go index e90ed4f..be8c6e8 100644 --- a/core/configuration_validator_test.go +++ b/core/configuration_validator_test.go @@ -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") diff --git a/docs/schema.json b/docs/schema.json index bf311ad..e3fbf01 100644 --- a/docs/schema.json +++ b/docs/schema.json @@ -14,7 +14,8 @@ "type": "string", "enum": [ "SlashStar", - "SlashSlash" + "SlashSlash", + "Hash" ] }, "includes": {