Skip to content

Commit

Permalink
Implement coverage target support (#109)
Browse files Browse the repository at this point in the history
* Implement coverage target support
  • Loading branch information
finomen authored Aug 3, 2022
1 parent 090b581 commit e8af870
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ const (
modeBuild mode = 1
modeRun mode = 2
modeTest mode = 3
modeCoverage mode = 4
)

type target struct {
Description string
Runnable bool
Testable bool
Report bool
}

type flag struct {
Expand All @@ -149,6 +151,7 @@ type generatorInput struct {
RunArgs []string
TestArgs []string
Layout string
SelectedTargets []string

// These fields are used by dbt-rules < v1.10.0 and must be kept for backward compatibility
Version uint
Expand Down Expand Up @@ -235,6 +238,8 @@ func runBuild(args []string, mode mode, modeArgs []string) {
genInput.RunArgs = modeArgs
case modeTest:
genInput.TestArgs = modeArgs
case modeCoverage:
genInput.TestArgs = modeArgs
}
genOutput := runGenerator(genInput)

Expand All @@ -255,10 +260,12 @@ func runBuild(args []string, mode mode, modeArgs []string) {
regexps = append(regexps, re)
}
targets := []string{}

for name, target := range genOutput.Targets {
if skipTarget(mode, target) {
continue
}

for _, re := range regexps {
if re.MatchString(name) {
targets = append(targets, name)
Expand All @@ -267,6 +274,13 @@ func runBuild(args []string, mode mode, modeArgs []string) {
}
}

if mode == modeCoverage {
// Second pass with all targets
genInput.SelectedTargets = targets
genOutput = runGenerator(genInput)
}


// Write the Ninja build file.
ninjaFilePath := path.Join(genInput.OutputDir, ninjaFileName)
log.Debug("Ninja file: %s.\n", ninjaFilePath)
Expand Down Expand Up @@ -338,6 +352,7 @@ func runBuild(args []string, mode mode, modeArgs []string) {
case modeTest:
suffix = "#test"
}

for _, target := range targets {
ninjaArgs = append(ninjaArgs, target+suffix)
}
Expand Down Expand Up @@ -638,6 +653,8 @@ func skipTarget(mode mode, target target) bool {
return !target.Runnable
case modeTest:
return !target.Testable
case modeCoverage:
return !target.Testable && !target.Report
}
return false
}
34 changes: 34 additions & 0 deletions cmd/coverage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"github.com/daedaleanai/cobra"
)

var coverageCmd = &cobra.Command{
Use: "coverage [patterns] [build flags] [: test args]",
Short: "Builds, tests the targets and generate coverage report.",
Long: `Builds, tests the targets and generate coverage report.`,
Run: runCoverage,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeBuildArgs(toComplete, modeCoverage), cobra.ShellCompDirectiveNoFileComp
},
DisableFlagsInUseLine: true,
}

func init() {
rootCmd.AddCommand(coverageCmd)
coverageCmd.Flags().SetInterspersed(false)
}

func runCoverage(cmd *cobra.Command, args []string) {
testArgs := []string{}
buildArgs := args
for idx, arg := range args {
if arg == ":" {
testArgs = args[idx+1:]
buildArgs = args[:idx]
break
}
}
runBuild(buildArgs, modeCoverage, testArgs)
}

0 comments on commit e8af870

Please sign in to comment.