forked from thought-machine/please
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade original targets (thought-machine#2942)
* Add targetset * start using it * Add AllTargets * Update a couple of tests * Add tests * Preserve original ordering * Exclude gocritic for test files, my tests don't need any more criticism * Version bump * Fix IsOriginalTarget logic * goddamn tabses, we hates them
- Loading branch information
1 parent
90382a7
commit e1028b9
Showing
10 changed files
with
205 additions
and
74 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
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
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 |
---|---|---|
@@ -1 +1 @@ | ||
17.4.0-beta.3 | ||
17.4.0-beta.4 |
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
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
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
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
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
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,63 @@ | ||
package core | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// A TargetSet contains a series of targets and supports efficiently checking for membership | ||
// The zero value is not safe for use. | ||
type TargetSet struct { | ||
targets map[BuildLabel]struct{} | ||
packages map[packageKey]struct{} | ||
everything []BuildLabel | ||
mutex sync.RWMutex | ||
} | ||
|
||
// NewTargetSet returns a new TargetSet. | ||
func NewTargetSet() *TargetSet { | ||
return &TargetSet{ | ||
targets: map[BuildLabel]struct{}{}, | ||
packages: map[packageKey]struct{}{}, | ||
} | ||
} | ||
|
||
// Add adds a new target to this set. | ||
func (ts *TargetSet) Add(label BuildLabel) { | ||
ts.mutex.Lock() | ||
defer ts.mutex.Unlock() | ||
if label.IsAllSubpackages() { | ||
panic("TargetSet doesn't support ... labels") | ||
} else if label.IsAllTargets() { | ||
ts.packages[label.packageKey()] = struct{}{} | ||
} else { | ||
ts.targets[label] = struct{}{} | ||
} | ||
ts.everything = append(ts.everything, label) | ||
} | ||
|
||
// Match checks if this label is covered by the set (either explicitly or via :all) | ||
// The first return checks if it's matched, the second if the match was exact or not. | ||
func (ts *TargetSet) Match(label BuildLabel) (bool, bool) { | ||
ts.mutex.RLock() | ||
defer ts.mutex.RUnlock() | ||
if _, present := ts.targets[label]; present { | ||
return true, true | ||
} | ||
_, present := ts.packages[label.packageKey()] | ||
return present, false | ||
} | ||
|
||
// MatchExact checks if this label was explicitly added to the set (i.e. :all doesn't count) | ||
func (ts *TargetSet) MatchExact(label BuildLabel) bool { | ||
ts.mutex.RLock() | ||
defer ts.mutex.RUnlock() | ||
_, present := ts.targets[label] | ||
return present | ||
} | ||
|
||
// AllTargets returns a copy of the set of targets | ||
func (ts *TargetSet) AllTargets() []BuildLabel { | ||
ts.mutex.RLock() | ||
defer ts.mutex.RUnlock() | ||
return ts.everything[:] | ||
} |
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,53 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTargetSetMatch(t *testing.T) { | ||
ts := NewTargetSet() | ||
ts.Add(ParseBuildLabel("//src/core:core", "")) | ||
ts.Add(ParseBuildLabel("//src/parse:all", "")) | ||
matched, exact := ts.Match(ParseBuildLabel("//src/core:core", "")) | ||
assert.True(t, matched) | ||
assert.True(t, exact) | ||
matched, exact = ts.Match(ParseBuildLabel("//src/core:core_test", "")) | ||
assert.False(t, matched) | ||
assert.False(t, exact) | ||
matched, exact = ts.Match(ParseBuildLabel("//src/parse:parse", "")) | ||
assert.True(t, matched) | ||
assert.False(t, exact) | ||
matched, exact = ts.Match(ParseBuildLabel("//src/parse:parse_test", "")) | ||
assert.True(t, matched) | ||
assert.False(t, exact) | ||
matched, exact = ts.Match(ParseBuildLabel("//src/build", "")) | ||
assert.False(t, matched) | ||
assert.False(t, exact) | ||
} | ||
|
||
func TestTargetSetMatchExact(t *testing.T) { | ||
ts := NewTargetSet() | ||
ts.Add(ParseBuildLabel("//src/core:core", "")) | ||
ts.Add(ParseBuildLabel("//src/parse:all", "")) | ||
assert.True(t, ts.MatchExact(ParseBuildLabel("//src/core:core", ""))) | ||
assert.False(t, ts.MatchExact(ParseBuildLabel("//src/core:core_test", ""))) | ||
assert.False(t, ts.MatchExact(ParseBuildLabel("//src/parse:parse", ""))) | ||
assert.False(t, ts.MatchExact(ParseBuildLabel("//src/parse:parse_test", ""))) | ||
assert.False(t, ts.MatchExact(ParseBuildLabel("//src/build", ""))) | ||
} | ||
|
||
func TestAllTargets(t *testing.T) { | ||
ts := NewTargetSet() | ||
labels := []BuildLabel{ | ||
ParseBuildLabel("//src/core:core", ""), | ||
ParseBuildLabel("//src/core:core_test", ""), | ||
ParseBuildLabel("//src/parse:all", ""), | ||
ParseBuildLabel("//src/parse:parse_test", ""), | ||
} | ||
for _, label := range labels { | ||
ts.Add(label) | ||
} | ||
assert.Equal(t, labels, ts.AllTargets()) | ||
} |