This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from timstclair/testing
Add some builder unit tests
- Loading branch information
Showing
16 changed files
with
375 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |