Skip to content

Commit

Permalink
add ginkgo watch -watchRegExp fixes #356
Browse files Browse the repository at this point in the history
  • Loading branch information
onsi committed Jun 27, 2017
1 parent ed94a3a commit 15a58d2
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- `ginkgo` now provides a hint if you accidentally forget to run `ginkgo bootstrap` to generate a `*_suite_test.go` file that actually invokes the Ginkgo test runner. [#345](https://github.com/onsi/ginkgo/pull/345)
- thanks to improvements in `go test -c` `ginkgo` no longer needs to fix Go's compilation output to ensure compilation errors are expressed relative to the CWD. [#357]
- `ginkgo watch -watchRegExp=...` allows you to specify a custom regular expression to watch. Only files matching the regular expression are watched for changes (the default is `\.go$`)

## 1.3.0 3/28/2017

Expand Down
4 changes: 3 additions & 1 deletion ginkgo/run_watch_and_build_command_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type RunWatchAndBuildCommandFlags struct {
RandomizeSuites bool

//only for watch command
Depth int
Depth int
WatchRegExp string

FlagSet *flag.FlagSet
}
Expand Down Expand Up @@ -156,5 +157,6 @@ func (c *RunWatchAndBuildCommandFlags) flags(mode int) {

if mode == watchMode {
c.FlagSet.IntVar(&(c.Depth), "depth", 1, "Ginkgo will watch dependencies down to this depth in the dependency tree")
c.FlagSet.StringVar(&(c.WatchRegExp), "watchRegExp", `\.go$`, "Files matching this regular expression will be watched for changes")
}
}
8 changes: 6 additions & 2 deletions ginkgo/watch/delta_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ package watch
import (
"fmt"

"regexp"

"github.com/onsi/ginkgo/ginkgo/testsuite"
)

type SuiteErrors map[testsuite.TestSuite]error

type DeltaTracker struct {
maxDepth int
watchRegExp *regexp.Regexp
suites map[string]*Suite
packageHashes *PackageHashes
}

func NewDeltaTracker(maxDepth int) *DeltaTracker {
func NewDeltaTracker(maxDepth int, watchRegExp *regexp.Regexp) *DeltaTracker {
return &DeltaTracker{
maxDepth: maxDepth,
packageHashes: NewPackageHashes(),
watchRegExp: watchRegExp,
packageHashes: NewPackageHashes(watchRegExp),
suites: map[string]*Suite{},
}
}
Expand Down
15 changes: 8 additions & 7 deletions ginkgo/watch/package_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ import (
"time"
)

var goRegExp = regexp.MustCompile(`\.go$`)
var goTestRegExp = regexp.MustCompile(`_test\.go$`)

type PackageHash struct {
CodeModifiedTime time.Time
TestModifiedTime time.Time
Deleted bool

path string
codeHash string
testHash string
path string
codeHash string
testHash string
watchRegExp *regexp.Regexp
}

func NewPackageHash(path string) *PackageHash {
func NewPackageHash(path string, watchRegExp *regexp.Regexp) *PackageHash {
p := &PackageHash{
path: path,
path: path,
watchRegExp: watchRegExp,
}

p.codeHash, _, p.testHash, _, p.Deleted = p.computeHashes()
Expand Down Expand Up @@ -82,7 +83,7 @@ func (p *PackageHash) computeHashes() (codeHash string, codeModifiedTime time.Ti
continue
}

if goRegExp.Match([]byte(info.Name())) {
if p.watchRegExp.Match([]byte(info.Name())) {
codeHash += p.hashForFileInfo(info)
if info.ModTime().After(codeModifiedTime) {
codeModifiedTime = info.ModTime()
Expand Down
7 changes: 5 additions & 2 deletions ginkgo/watch/package_hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ package watch

import (
"path/filepath"
"regexp"
"sync"
)

type PackageHashes struct {
PackageHashes map[string]*PackageHash
usedPaths map[string]bool
watchRegExp *regexp.Regexp
lock *sync.Mutex
}

func NewPackageHashes() *PackageHashes {
func NewPackageHashes(watchRegExp *regexp.Regexp) *PackageHashes {
return &PackageHashes{
PackageHashes: map[string]*PackageHash{},
usedPaths: nil,
watchRegExp: watchRegExp,
lock: &sync.Mutex{},
}
}
Expand All @@ -41,7 +44,7 @@ func (p *PackageHashes) Add(path string) *PackageHash {
path, _ = filepath.Abs(path)
_, ok := p.PackageHashes[path]
if !ok {
p.PackageHashes[path] = NewPackageHash(path)
p.PackageHashes[path] = NewPackageHash(path, p.watchRegExp)
}

if p.usedPaths != nil {
Expand Down
3 changes: 2 additions & 1 deletion ginkgo/watch_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"regexp"
"time"

"github.com/onsi/ginkgo/config"
Expand Down Expand Up @@ -72,7 +73,7 @@ func (w *SpecWatcher) WatchSuites(args []string, additionalArgs []string) {
}

fmt.Printf("Identified %d test %s. Locating dependencies to a depth of %d (this may take a while)...\n", len(suites), pluralizedWord("suite", "suites", len(suites)), w.commandFlags.Depth)
deltaTracker := watch.NewDeltaTracker(w.commandFlags.Depth)
deltaTracker := watch.NewDeltaTracker(w.commandFlags.Depth, regexp.MustCompile(w.commandFlags.WatchRegExp))
delta, errors := deltaTracker.Delta(suites)

fmt.Printf("Watching %d %s:\n", len(delta.NewSuites), pluralizedWord("suite", "suites", len(delta.NewSuites)))
Expand Down
3 changes: 3 additions & 0 deletions integration/_fixtures/watch_fixtures/C/C.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"fixture": "data"
}
36 changes: 36 additions & 0 deletions integration/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ var _ = Describe("Watch", func() {
modifyFile(filepath.Join(rootPath, "src", "github.com", "onsi", pkgToModify, pkgToModify+".go"))
}

modifyJSON := func(pkgToModify string) {
modifyFile(filepath.Join(rootPath, "src", "github.com", "onsi", pkgToModify, pkgToModify+".json"))
}

modifyTest := func(pkgToModify string) {
modifyFile(filepath.Join(rootPath, "src", "github.com", "onsi", pkgToModify, pkgToModify+"_test.go"))
}
Expand Down Expand Up @@ -207,6 +211,38 @@ var _ = Describe("Watch", func() {
})
})

Describe("adjusting the watch regular expression", func() {
Describe("the default regular expression", func() {
It("should only trigger when go files are changed", func() {
session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=2")
Eventually(session).Should(gbytes.Say("Identified 3 test suites"))
Eventually(session).Should(gbytes.Say(`A \[2 dependencies\]`))
Eventually(session).Should(gbytes.Say(`B \[1 dependency\]`))
Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`))

modifyJSON("C")
Consistently(session).ShouldNot(gbytes.Say("Detected changes in"))
Consistently(session).ShouldNot(gbytes.Say("A Suite|B Suite|C Suite"))
})
})

Describe("modifying the regular expression", func() {
It("should trigger if the regexp matches", func() {
session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=2", `-watchRegExp=\.json$`)
Eventually(session).Should(gbytes.Say("Identified 3 test suites"))
Eventually(session).Should(gbytes.Say(`A \[2 dependencies\]`))
Eventually(session).Should(gbytes.Say(`B \[1 dependency\]`))
Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`))

modifyJSON("C")
Eventually(session).Should(gbytes.Say("Detected changes in"))
Eventually(session).Should(gbytes.Say("C Suite"))
Eventually(session).Should(gbytes.Say("B Suite"))
Eventually(session).Should(gbytes.Say("A Suite"))
})
})
})

Describe("when new test suite is added", func() {
It("should start monitoring that test suite", func() {
session = startGinkgoWithGopath("watch", "-succinct", "-r")
Expand Down

0 comments on commit 15a58d2

Please sign in to comment.