Skip to content

Commit

Permalink
feat: add --genearted option and add vcl_pipe related linter rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ysugimoto committed Jul 24, 2024
1 parent d817f05 commit 7d7699a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/falco/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ Flags:
-v : Output lint warnings (verbose)
-vv : Output all lint results (very verbose)
-json : Output results as JSON (very verbose)
--generated : Lint for Fastly generated VCL
Simple linting with very verbose example:
falco lint -I . -vv /path/to/vcl/main.vcl
Expand Down
35 changes: 28 additions & 7 deletions cmd/falco/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ func loadRepoExampleTestMetadata() []RepoExampleTestMetadata {
warnings: 0,
infos: 1,
},
{
name: "Fastly Generated",
fileName: "../../examples/linter/fastly_generated.vcl",
errors: 0,
warnings: 12,
infos: 2,
},
}

// Run custom linter testing only in CI env
Expand Down Expand Up @@ -406,3 +399,31 @@ func TestTester(t *testing.T) {
})
}
}

func TestFastlyGeneratedVCLLinting(t *testing.T) {
c, err := config.New([]string{"--generated"})
if err != nil {
t.Errorf("Unexpected config pointer generation error: %s", err)
return
}

resolvers, err := resolver.NewFileResolvers("../../examples/linter/fastly_generated.vcl", c.IncludePaths)
if err != nil {
t.Errorf("Unexpected runner creation error: %s", err)
return
}
ret, err := NewRunner(c, nil).Run(resolvers[0])
if err != nil {
t.Errorf("Unexpected linting error: %s", err)
return
}
if ret.Infos != 2 {
t.Errorf("Infos expects 2, got %d", ret.Infos)
}
if ret.Warnings != 3 {
t.Errorf("Warning expects 3, got %d", ret.Warnings)
}
if ret.Errors > 0 {
t.Errorf("Errors expects 0, got %d", ret.Errors)
}
}
1 change: 1 addition & 0 deletions config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var needValueOptions = map[string]struct{}{
"--transformer": {},
"-f": {},
"--filter": {},
"--generated": {},
}

func parseCommands(args []string) Commands {
Expand Down
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type LinterConfig struct {
Rules map[string]string `yaml:"rules"`
EnforceSubroutineScopes map[string][]string `yaml:"enforce_subroutine_scopes"`
IgnoreSubroutines []string `yaml:"ignore_subroutines"`
IsGenerated bool `cli:"generated"`
}

// Simulator configuration
Expand Down Expand Up @@ -177,6 +178,14 @@ func New(args []string) (*Config, error) {
// On Fastly generated VCL, "vcl_pipe" subroutine will present internally.
// The "vcl_pipe" subroutine looks fastly managed but undocumented, so we will ignore linting
c.Linter.IgnoreSubroutines = append(c.Linter.IgnoreSubroutines, "vcl_pipe")
// If generated option provided for linter, modify default rules
if c.Linter.IsGenerated {
if c.Linter.Rules == nil {
c.Linter.Rules = make(map[string]string)
}
// Fastly generated VCL no longer has boiler-plate marco due to extracted so ignore it
c.Linter.Rules["subroutine/boilerplate-macro"] = "IGNORE" // TODO: use linter rule constants instead of string hard-coded
}

return c, nil
}
Expand Down
10 changes: 10 additions & 0 deletions linter/declaration_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ func (l *Linter) lintSubRoutineDeclaration(decl *ast.SubroutineDeclaration, ctx
if !isValidName(decl.Name.Value) {
l.Error(InvalidName(decl.Name.GetMeta(), decl.Name.Value, "sub").Match(SUBROUTINE_SYNTAX))
}
// vcl_pipe lifecycle subroutine is reserved in Fastly generated VCL.
// When the user specify this name of subroutine, Fastly prevents to generated their own vcl_pipe subroutine.
// It causes unexpected behavior so falco should report it to not to break original behavior (switch to pipe-mode in Varnish).
if decl.Name.Value == "vcl_pipe" {
l.Error((&LintError{
Severity: WARNING,
Token: decl.Name.GetMeta().Token,
Message: "user-defiend vcl_pipe subroutine may cause unexpected behavior in Fastly actual runtime",
}).Match(FORBID_VCL_PIPE))
}

scope := getSubroutineCallScope(decl)
if scope == -1 {
Expand Down
7 changes: 7 additions & 0 deletions linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,3 +869,10 @@ sub ignored_subroutine {
assertNoError(t, input)
})
}

func TestForbidVclPipeSubroutine(t *testing.T) {
input := `
sub vcl_pipe {}
`
assertError(t, input)
}
1 change: 1 addition & 0 deletions linter/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
SUBROUTINE_DUPLICATED = "subroutine/duplicated"
SUBROUTINE_INVALID_RETURN_TYPE = "subroutine/invalid-return-type"
UNRECOGNIZE_CALL_SCOPE = "subroutine/unrecognize-call-scope"
FORBID_VCL_PIPE = "subroutine/forbid-vcl-pipe"
PENALTYBOX_SYNTAX = "penaltybox/syntax"
PENALTYBOX_DUPLICATED = "penaltybox/duplicated"
PENALTYBOX_NONEMPTY_BLOCK = "penaltybox/nonempty-block"
Expand Down

0 comments on commit 7d7699a

Please sign in to comment.