Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #15 from timstclair/testing
Browse files Browse the repository at this point in the history
Add some builder unit tests
  • Loading branch information
tallclair authored Aug 12, 2016
2 parents 7b2d633 + 5c430f9 commit 0273292
Show file tree
Hide file tree
Showing 16 changed files with 375 additions and 0 deletions.
167 changes: 167 additions & 0 deletions pkg/deps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package pkg

import (
"go/build"
"regexp"
"testing"

"github.com/stretchr/testify/assert"
)

var (
// Expected import layout. All names (except stdDeps) are relative to
// "github.com/google/godepq/testing"
expectations = map[string][]string{
"": []string{"a", "b"},
"a": []string{"a/aa", "a/ab"},
"a/aa": []string{"a/aa/aaa"},
"a/aa/aaa": nil,
"a/ab": nil,
"b": []string{"b/ba"},
"b/ba": nil,
}
)

const basePkg = "github.com/google/godepq/testing"

func TestBuildBare(t *testing.T) {
testBuildBasic(t, false, false)
}

func TestBuildWithStdlib(t *testing.T) {
testBuildBasic(t, true, false)
}

func TestBuildWithTests(t *testing.T) {
testBuildBasic(t, false, true)
}

func testBuildBasic(t *testing.T, includeStdlib, includeTests bool) {
deps, err := (&Builder{
Roots: []Package{Package(basePkg)},
BuildContext: build.Default,
IncludeStdlib: includeStdlib,
IncludeTests: includeTests,
}).Build()
assert.NoError(t, err)
assertGraphsEqual(t, deps.Forward, expectedGraph(includeStdlib, includeTests))
assert.Len(t, deps.Ignored, 0)
}

func TestIgnoreBasic(t *testing.T) {
deps, err := (&Builder{
Roots: []Package{Package(basePkg)},
BuildContext: build.Default,
Ignored: []*regexp.Regexp{
regexp.MustCompile(basePkg + "/b.*"),
},
}).Build()
assert.NoError(t, err)

// Build expected graph without "b" packages.
expected := expectedGraph(false, false)
delete(expected, mkpkg("b"))
delete(expected, mkpkg("b/ba"))
delete(expected.Pkg(mkpkg("")), mkpkg("b"))

assertGraphsEqual(t, deps.Forward, expected)
expectedIgnores := NewSet(mkpkg("b")) // Never reach "b/ba".
assertSetsEqual(t, deps.Ignored, expectedIgnores, "Ignored")
}

func TestIgnoreWithStdlibAndTests(t *testing.T) {
deps, err := (&Builder{
Roots: []Package{Package(basePkg)},
BuildContext: build.Default,
Ignored: []*regexp.Regexp{
regexp.MustCompile("err?ors"), // Ignore stdlib
regexp.MustCompile("github.com/.*/c"), // Ignore tests
},
IncludeStdlib: true,
IncludeTests: true,
}).Build()
assert.NoError(t, err)
assertGraphsEqual(t, deps.Forward, expectedGraph(false, false))
expectedIgnores := NewSet(mkpkg("c"), Package("errors"))
assertSetsEqual(t, deps.Ignored, expectedIgnores, "Ignored")
}

func TestInclude(t *testing.T) {
deps, err := (&Builder{
Roots: []Package{Package(basePkg)},
BuildContext: build.Default,
Included: []*regexp.Regexp{
regexp.MustCompile("^" + basePkg + "$"),
regexp.MustCompile("github.com/.*/[ab].*"), // Ignore tests
},
IncludeStdlib: true,
IncludeTests: true,
}).Build()
assert.NoError(t, err)
assertGraphsEqual(t, deps.Forward, expectedGraph(false, false))
expectedIgnores := NewSet(mkpkg("c"), Package("errors"))
assertSetsEqual(t, deps.Ignored, expectedIgnores, "Ignored")
}

func mkpkg(rel string) Package {
if rel == "" {
return Package(basePkg)
}
return Package(basePkg + "/" + rel)
}

func expectedGraph(includeStdlib, includeTests bool) Graph {
expected := NewGraph()
for pkg, imports := range expectations {
imps := expected.Pkg(mkpkg(pkg))
for _, imp := range imports {
imps.Insert(mkpkg(imp))
}
if includeStdlib && pkg != "" {
imps.Insert(Package("errors"))
}
if includeTests && pkg != "" {
imps.Insert(mkpkg("c"))
}
}
if includeStdlib {
expected.Pkg(Package("errors"))
}
if includeTests {
imps := expected.Pkg(mkpkg("c"))
if includeStdlib {
imps.Insert(Package("errors"))
}
}
return expected
}

func assertGraphsEqual(t *testing.T, actual, expected Graph) {
// Check for missing packages.
for pkg, imports := range expected {
assertSetsEqual(t, actual[pkg], imports, string(pkg))
}
// Check for unexpected packages.
for pkg := range actual {
assert.Contains(t, expected, pkg, "Unexpected package %s", pkg)
}
}

func assertSetsEqual(t *testing.T, actual, expected Set, ctx string) {
// Check for missing items.
for k := range expected {
assert.Contains(t, actual, k, "[%s] Missing expected item %s", ctx, k)
}
// Check for unexpected items.
for k := range actual {
assert.Contains(t, expected, k, "[%s] Unexpected item %s", ctx, k)
}
}
13 changes: 13 additions & 0 deletions testing/a/aa/aaa/imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package aaa

import (
_ "errors"
)
13 changes: 13 additions & 0 deletions testing/a/aa/aaa/imports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package aaa

import (
_ "github.com/google/godepq/testing/c"
)
15 changes: 15 additions & 0 deletions testing/a/aa/imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package aa

import (
_ "errors"

_ "github.com/google/godepq/testing/a/aa/aaa"
)
13 changes: 13 additions & 0 deletions testing/a/aa/imports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package aa

import (
_ "github.com/google/godepq/testing/c"
)
13 changes: 13 additions & 0 deletions testing/a/ab/imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package ab

import (
_ "errors"
)
13 changes: 13 additions & 0 deletions testing/a/ab/imports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package ab

import (
_ "github.com/google/godepq/testing/c"
)
16 changes: 16 additions & 0 deletions testing/a/imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package a

import (
_ "errors"

_ "github.com/google/godepq/testing/a/aa"
_ "github.com/google/godepq/testing/a/ab"
)
13 changes: 13 additions & 0 deletions testing/a/imports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package a

import (
_ "github.com/google/godepq/testing/c"
)
13 changes: 13 additions & 0 deletions testing/b/ba/imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package ba

import (
_ "errors"
)
13 changes: 13 additions & 0 deletions testing/b/ba/imports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package ba

import (
_ "github.com/google/godepq/testing/c"
)
15 changes: 15 additions & 0 deletions testing/b/imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package b

import (
_ "errors"

_ "github.com/google/godepq/testing/b/ba"
)
13 changes: 13 additions & 0 deletions testing/b/imports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package b

import (
_ "github.com/google/godepq/testing/c"
)
13 changes: 13 additions & 0 deletions testing/c/imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package c

import (
_ "errors"
)
18 changes: 18 additions & 0 deletions testing/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

// This package sets up an import DAG for testing purposes.
// The structure looks like:
// a b c
// / \ |
// aa ab ba
// |
// aaa
//
// With every test depending on c, and every non-test depending on "errors".
package testing
14 changes: 14 additions & 0 deletions testing/imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

package testing

import (
_ "github.com/google/godepq/testing/a"
_ "github.com/google/godepq/testing/b"
)

0 comments on commit 0273292

Please sign in to comment.