Skip to content

Commit

Permalink
Merge pull request #84 from coreruleset/add-default-comment-header-da…
Browse files Browse the repository at this point in the history
…ta-files

feat: add check for standard regex assembly header
  • Loading branch information
fzipi authored May 21, 2023
2 parents 5947c16 + f272e60 commit 4c1b800
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 35 deletions.
21 changes: 20 additions & 1 deletion cmd/error.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
package cmd

import "errors"
import (
"errors"
"fmt"
)

var (
ErrUpdateCopyrightWithoutVersion = errors.New("version is needed to update the copyright. You can use 'git describe --tags' if using git")
)

type UnformattedFileError struct {
filePath string
}

func (u *UnformattedFileError) Error() string {
if u.HasPathInfo() {
return fmt.Sprintf("File not properly formatted: %s", u.filePath)
}

return "One or more files are not properly formatted"
}

func (u *UnformattedFileError) HasPathInfo() bool {
return u.filePath != ""
}
30 changes: 15 additions & 15 deletions cmd/regex_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,7 @@ import (
"github.com/coreruleset/crs-toolchain/regex/processors"
)

type UnformattedFileError struct {
filePath string
}

func (u *UnformattedFileError) Error() string {
if u.HasPathInfo() {
return fmt.Sprintf("File not properly formatted: %s", u.filePath)
}

return "One or more files are not properly formatted"
}

func (u *UnformattedFileError) HasPathInfo() bool {
return u.filePath != ""
}
const regexAssemblyStandardHeader = "##! Please refer to the documentation at\n##! https://coreruleset.org/docs/development/crs_toolchain/.\n"

// formatCmd represents the generate command
var formatCmd = createFormatCommand()
Expand Down Expand Up @@ -177,6 +163,7 @@ func processFile(filePath string, ctxt *processors.Context, checkOnly bool) erro
logger.Error().Err(err).Msgf("failed to open file %s", filePath)
return err
}

parser := parser.NewParser(ctxt, file)
parsedBytes, _ := parser.Parse(true)
if err = file.Close(); err != nil {
Expand All @@ -198,6 +185,11 @@ func processFile(filePath string, ctxt *processors.Context, checkOnly bool) erro
lines = append(lines, string(line))
}

if checkStandardHeader(lines) {
logger.Info().Msgf("file %s does not have standard header", filePath)
// prepend the standard header
lines = append([]string{regexAssemblyStandardHeader}, lines...)
}
lines = formatEndOfFile(lines)

newContents := []byte(strings.Join(lines, "\n"))
Expand Down Expand Up @@ -291,3 +283,11 @@ func formatEndOfFile(lines []string) []string {
// to the others by newline
return append(lines[:eof+1], "")
}

func checkStandardHeader(lines []string) bool {
if len(lines) > 2 &&
lines[0]+lines[1] == regexAssemblyStandardHeader {
return true
}
return false
}
39 changes: 20 additions & 19 deletions cmd/regex_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s *formatTestSuite) TestFormat_TrimsTabs() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `line1
expected := regexAssemblyStandardHeader + `line1
line2
`
output := s.readDataFile("123456.ra")
Expand All @@ -111,7 +111,7 @@ func (s *formatTestSuite) TestFormat_TrimsSpaces() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `line1
expected := regexAssemblyStandardHeader + `line1
line2
`
output := s.readDataFile("123456.ra")
Expand All @@ -126,7 +126,7 @@ func (s *formatTestSuite) TestFormat_IndentsAssembleBlock() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `##!> assemble
expected := regexAssemblyStandardHeader + `##!> assemble
line
##!<
`
Expand Down Expand Up @@ -154,7 +154,7 @@ func (s *formatTestSuite) TestFormat_IndentsNestedAssembleBlocks() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `##!> assemble
expected := regexAssemblyStandardHeader + `##!> assemble
line
##!> assemble
##!=> output
Expand Down Expand Up @@ -182,7 +182,7 @@ func (s *formatTestSuite) TestFormat_EndOfFileHasNewLineAfterNoNewLine() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `##!> assemble
expected := regexAssemblyStandardHeader + `##!> assemble
line
##!<
`
Expand All @@ -199,7 +199,7 @@ func (s *formatTestSuite) TestFormat_EndOfFileHasNewLineAfterOneNewLine() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `##!> assemble
expected := regexAssemblyStandardHeader + `##!> assemble
line
##!<
`
Expand All @@ -217,7 +217,7 @@ func (s *formatTestSuite) TestFormat_EndOfFileHasNewLineAfterTwoNewLines() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `##!> assemble
expected := regexAssemblyStandardHeader + `##!> assemble
line
##!<
`
Expand All @@ -231,7 +231,7 @@ func (s *formatTestSuite) TestFormat_EndOfFileHasNewLineIfEmpty() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := "\n"
expected := regexAssemblyStandardHeader
output := s.readDataFile("123456.ra")
s.Equal(expected, output)
}
Expand All @@ -248,7 +248,7 @@ func (s *formatTestSuite) TestFormat_DoesNotRemoveEmptyLines() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `
expected := regexAssemblyStandardHeader + `
##!> assemble
Expand All @@ -272,7 +272,7 @@ func (s *formatTestSuite) TestFormat_DoesNotRemoveComments() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `
expected := regexAssemblyStandardHeader + `
##! a comment
##!> assemble
##! a comment
Expand All @@ -299,7 +299,7 @@ func (s *formatTestSuite) TestFormat_OnlyIndentsAssembleProcessor() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `##!> assemble
expected := regexAssemblyStandardHeader + `##!> assemble
##!> include bart
##!> assemble
##!+ i
Expand All @@ -325,7 +325,7 @@ func (s *formatTestSuite) TestFormat_FormatsProcessors() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `##!> assemble
expected := regexAssemblyStandardHeader + `##!> assemble
##!> include bart
##!> cmdline windows
##!> define homer simpson
Expand All @@ -345,7 +345,7 @@ func (s *formatTestSuite) TestFormat_FormatsFlags() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `##!+ i
expected := regexAssemblyStandardHeader + `##!+ i
##!+ i
##!+ i
`
Expand All @@ -362,7 +362,7 @@ func (s *formatTestSuite) TestFormat_FormatsPrefix() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `##!^ prefix without separating white space
expected := regexAssemblyStandardHeader + `##!^ prefix without separating white space
##!^ prefix with leading white space
##!^ prefix with trailing white space
`
Expand All @@ -379,7 +379,7 @@ func (s *formatTestSuite) TestFormat_FormatsSuffix() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `##!$ suffix without separating white space
expected := regexAssemblyStandardHeader + `##!$ suffix without separating white space
##!$ suffix with leading white space
##!$ suffix with trailing white space
`
Expand All @@ -396,7 +396,7 @@ func (s *formatTestSuite) TestFormat_FormatsDefinitions() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `##!> define without-separating-white-space homer
expected := regexAssemblyStandardHeader + `##!> define without-separating-white-space homer
##!> define with-leading-white-space homer
##!> define with-trailing-white-space homer
`
Expand All @@ -413,7 +413,7 @@ func (s *formatTestSuite) TestFormat_FormatsIncludes() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `##!> include without-separating-white-space
expected := regexAssemblyStandardHeader + `##!> include without-separating-white-space
##!> include with-leading-white-space
##!> include with-trailing-white-space
`
Expand All @@ -430,7 +430,7 @@ func (s *formatTestSuite) TestFormat_FormatsExcept() {
_, err := rootCmd.ExecuteC()
s.NoError(err)

expected := `##!> include-except without-separating-white-space homer
expected := regexAssemblyStandardHeader + `##!> include-except without-separating-white-space homer
##!> include-except with-leading-white-space homer
##!> include-except with-trailing-white-space homer
`
Expand All @@ -439,7 +439,8 @@ func (s *formatTestSuite) TestFormat_FormatsExcept() {
}

func (s *formatTestSuite) writeDataFile(filename string, contents string) {
err := os.WriteFile(path.Join(s.dataDir, filename), []byte(contents), fs.ModePerm)
contentsWithHeader := regexAssemblyStandardHeader + contents
err := os.WriteFile(path.Join(s.dataDir, filename), []byte(contentsWithHeader), fs.ModePerm)
s.NoError(err)
}

Expand Down

0 comments on commit 4c1b800

Please sign in to comment.