Compare commits

..

8 Commits

Author SHA1 Message Date
John Donaldson 00cc3dc4ef Fix pointlog tests 2022-10-28 14:42:03 -07:00
John Donaldson 44a79c4b6e Lots of smaller tests to improve coverage 2022-10-28 14:38:57 -07:00
John Donaldson 1f52fddd35 Better hide coverage files 2022-10-28 13:24:31 -07:00
John Donaldson 6187ab4968 Adding more state filesystem checks 2022-10-28 13:22:23 -07:00
John Donaldson bfacbfeb11 Fixing scope of test variables 2022-10-28 12:46:47 -07:00
John Donaldson 63d066c195 Add some team ID checks for better coverage 2022-10-28 12:44:18 -07:00
John Donaldson b978903f93 Pull XML reporting into its own job, so it still shows up 2022-10-28 19:27:20 +00:00
John Donaldson 966dfad5d7 Coverage xml wasn't getting added, now 2022-10-28 12:15:00 -07:00
4 changed files with 253 additions and 10 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
.idea .idea
/vendor/ /vendor/
__debug_bin __debug_bin
coverage.*

View File

@ -2,31 +2,42 @@ stages:
- test - test
- push - push
test: Run unit tests:
stage: test stage: test
image: &goimage golang:1.18
image: golang:1.18
only: only:
refs: refs:
- main - main
- merge_requests - merge_requests
script: script:
- go test -coverprofile=coverage.txt -covermode=atomic -race ./... - go test -coverprofile=coverage.txt -covermode=atomic -race ./...
- go get github.com/boumenot/gocover-cobertura
- go run github.com/boumenot/gocover-cobertura < coverage.txt > coverage.xml
- go tool cover -html=coverage.txt -o coverage.html - go tool cover -html=coverage.txt -o coverage.html
- go tool cover -func coverage.txt - go tool cover -func coverage.txt
coverage: /\(statements\)(?:\s+)?(\d+(?:\.\d+)?%)/ coverage: /\(statements\)(?:\s+)?(\d+(?:\.\d+)?%)/
artifacts:
paths:
- coverage.html
- coverage.txt
Generage coverage XML:
stage: test
image: *goimage
needs: ["Run unit tests"]
script:
- go get github.com/boumenot/gocover-cobertura
- go run github.com/boumenot/gocover-cobertura < coverage.txt > coverage.xml
only:
refs:
- main
- merge_requests
artifacts: artifacts:
reports: reports:
coverage_report: coverage_report:
coverage_format: cobertura coverage_format: cobertura
path: coverage.xml path: coverage.xml
paths:
- coverage.html
- coverage.txt
push: push:
stage: push stage: push
needs: ["Run unit tests"]
rules: rules:
- if: $CI_COMMIT_TAG - if: $CI_COMMIT_TAG
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'

View File

@ -315,7 +315,7 @@ func (s *State) SetTeamNames(teams map[string]string) error {
} }
func (s *State) TeamIDFromName(teamName string) (string, error) { func (s *State) TeamIDFromName(teamName string) (string, error) {
for name, id := range s.TeamNames() { for id, name := range s.TeamNames() {
if name == teamName { if name == teamName {
return id, nil return id, nil
} }

View File

@ -138,6 +138,17 @@ func TestState(t *testing.T) {
} }
// Various point-specific tests
func TestStatePointsEmpty(t *testing.T) {
s := NewTestState()
s.refresh()
// Points shouldn't exist, right now
if exists := s.PointExists("team1", "category", 1); exists {
t.Error("Point entry reported to exist, when it shouldn't")
}
}
func TestStatePointsRemoval(t *testing.T) { func TestStatePointsRemoval(t *testing.T) {
s := NewTestState() s := NewTestState()
s.refresh() s.refresh()
@ -235,6 +246,10 @@ func TestStatePointsRemovalAtTime(t *testing.T) {
t.Errorf("Received unexpected points entry: %s", pointsLog[0]) t.Errorf("Received unexpected points entry: %s", pointsLog[0])
} }
if exists := s.PointExistsAtTime(team, category, points1, time1); ! exists {
t.Error("Expected to find matching result, found none, instead")
}
s.AwardPointsAtTime(team, category, points2, time2) s.AwardPointsAtTime(team, category, points2, time2)
s.refresh() s.refresh()
@ -252,6 +267,17 @@ func TestStatePointsRemovalAtTime(t *testing.T) {
t.Errorf("Expected 2 point in the log, got %d", pointsLogLength) t.Errorf("Expected 2 point in the log, got %d", pointsLogLength)
} }
// Test that we bail out early, since the point log is sorted
if exists := s.PointExistsAtTime(team, category, points2+200, time1-10); exists {
t.Errorf("Expected to find no matching result, found something, instead")
}
// Or if the record just doesn't exist
if exists := s.PointExistsAtTime(team, category, points2, time2+10); exists {
t.Errorf("Expected to find matching result, found something, instead")
}
// Test removing points
s.RemovePointsAtTime(team, category, points1, time1) s.RemovePointsAtTime(team, category, points1, time1)
s.refresh() s.refresh()
@ -265,6 +291,10 @@ func TestStatePointsRemovalAtTime(t *testing.T) {
t.Errorf("Found unexpected points log entry: %s", pointsLog[0]) t.Errorf("Found unexpected points log entry: %s", pointsLog[0])
} }
if exists := s.PointExistsAtTime(team, category, points1, time1); exists {
t.Error("Expected to find no result, found results, instead")
}
s.RemovePointsAtTime(team, category, points1, time1) s.RemovePointsAtTime(team, category, points1, time1)
s.refresh() s.refresh()
@ -554,6 +584,15 @@ func TestStateTeamIDs(t *testing.T) {
} }
} }
// Check if an ID exists in an empty list
if teamIDExists, err := s.TeamIDExists(teamID1); err != nil {
t.Errorf("Received unexpected error %s", err)
if teamIDExists {
t.Errorf("Expected to receive false, since team ID list should be empty, but received true, instead")
}
}
// Add a team ID // Add a team ID
if err := s.AddTeamID(teamID1); err != nil { if err := s.AddTeamID(teamID1); err != nil {
t.Errorf("Received unexpected error %s", err) t.Errorf("Received unexpected error %s", err)
@ -594,6 +633,28 @@ func TestStateTeamIDs(t *testing.T) {
} }
} }
// Add a duplicate team ID
if err := s.AddTeamID(teamID2); err == nil {
t.Errorf("Expected to raise error, received nil, instead")
}
s.refresh()
if teamIDs, err := s.TeamIDs(); err != nil {
t.Errorf("Received unexpected error %s", err)
} else {
if len(teamIDs) != 2 {
t.Errorf("Expected to find 2 team IDs, found %d (%s), instead", len(teamIDs), teamIDs)
} else {
if exists1, err1 := s.TeamIDExists(teamID1); err1 != nil {
t.Errorf("Received unexpected error %s", err)
} else {
if ! exists1 {
t.Errorf("Expected to find team ID '%s', but didn't find it", teamID1)
}
}
}
}
// Remove a team ID // Remove a team ID
if err := s.RemoveTeamID(teamID1); err != nil { if err := s.RemoveTeamID(teamID1); err != nil {
t.Errorf("Received unexpected error %s", err) t.Errorf("Received unexpected error %s", err)
@ -633,6 +694,8 @@ func TestStateDeleteTeamIDList(t *testing.T) {
s := NewTestState() s := NewTestState()
s.refresh() s.refresh()
teamID1 := "foobar"
s.Fs.Remove("teamids.txt") s.Fs.Remove("teamids.txt")
teamIDs, err := s.TeamIDs() teamIDs, err := s.TeamIDs()
@ -642,17 +705,185 @@ func TestStateDeleteTeamIDList(t *testing.T) {
} }
if err == nil { if err == nil {
t.Errorf("Did not receive expected error for non-existent teamids.txt") t.Error("Did not receive expected error for non-existent teamids.txt")
} }
if err := s.AddTeamID(teamID1); err == nil {
t.Error("Expected to receive error when adding team with no teamids.txt, received nil, instead")
}
if err := s.RemoveTeamID(teamID1); err == nil {
t.Error("Expected to receive error when removing team with no teamids.txt, received nil, instead")
}
if _, err := s.TeamIDExists(teamID1); err == nil {
t.Error("Expected to receive error when checking team ID with no teamids.txt, received nil, instead")
}
}
func TestStatePermissionError(t *testing.T) {
t.Skip("Skipping because of how hard it is to tease out this error state")
s := NewTestState()
s.refresh()
emptyTeams := make([]string, 0)
if err := s.writeTeamIDs(emptyTeams); err != nil {
t.Errorf("Unexpected error when initializing teamids.txt, %s", err)
}
s.Fs.Chmod("teamids.txt", 0100)
if err := s.writeTeamIDs(emptyTeams); err == nil {
t.Error("Expected to receive error when making a bad write to teamids.txt, received nil, instead")
}
} }
func TestStateTeamNames(t *testing.T) { func TestStateTeamNames(t *testing.T) {
s := NewTestState() s := NewTestState()
s.refresh() s.refresh()
teamID1 := "foo"
teamID2 := "bar"
teamName1 := "baz"
teamName2 := "bat"
emptyTeams := make(map[string]string)
populatedTeams := make(map[string]string)
populatedTeams[teamID1] = teamName1
populatedTeams[teamID2] = teamName2
s.AddTeamID(teamID1)
s.AddTeamID(teamID2)
if teamNames := s.TeamNames(); len(teamNames) != 0 { if teamNames := s.TeamNames(); len(teamNames) != 0 {
t.Errorf("Expected to find 0 registered teams, found %d (%s), instead", len(teamNames), teamNames) t.Errorf("Expected to find 0 registered teams, found %d (%s), instead", len(teamNames), teamNames)
} }
// Try setting team names to an existing list
if err := s.SetTeamNames(populatedTeams); err != nil {
t.Errorf("Unexpected error when setting team names: %s", err)
} else {
s.refresh()
if teamNames := s.TeamNames(); len(teamNames) != len(populatedTeams) {
t.Errorf("Expected to find %d registered teams, found %d (%s), instead", len(populatedTeams), len(teamNames), teamNames)
}
}
// Try setting team names to an empty list
if err := s.SetTeamNames(emptyTeams); err != nil {
t.Errorf("Unexpected error when setting team names: %s", err)
} else {
s.refresh()
if teamNames := s.TeamNames(); len(teamNames) != len(emptyTeams) {
t.Errorf("Expected to find %d registered teams, found %d (%s), instead", len(emptyTeams), len(teamNames), teamNames)
}
}
// Try adding team names
if err := s.SetTeamName(teamID1, teamName1); err != nil {
t.Errorf("Unexpected error when adding team name (%s)", err)
} else {
s.refresh()
if teamNames := s.TeamNames(); len(teamNames) != 1 {
t.Errorf("Expected to find 1 registered team, found %d (%s), instead", len(teamNames), teamNames)
}
}
if err := s.SetTeamName(teamID2, teamName2); err != nil {
t.Errorf("Unexpected error when adding team name (%s)", err)
} else {
s.refresh()
if teamNames := s.TeamNames(); len(teamNames) != 2 {
t.Errorf("Expected to find 2 registered teams, found %d (%s), instead", len(teamNames), teamNames)
}
}
// Try adding a team that already exists
if err := s.SetTeamName(teamID2, teamName2); err == nil {
t.Error("Expected error when registering team, received nil, instead")
}
s.refresh()
if teamNames := s.TeamNames(); len(teamNames) != 2 {
t.Errorf("Expected to find 2 registered teams, found %d (%s), instead", len(teamNames), teamNames)
}
// Can we look up our team IDs?
s.refresh()
if teamID, err := s.TeamIDFromName(teamName1); err != nil {
t.Errorf("Unexpected error (%s)", err)
} else {
if teamID != teamID1 {
t.Errorf("Expected to retrieve team ID (%s), received (%s), instead", teamID1, teamID)
}
}
// And taking them away
if err := s.DeleteTeamName(teamID1); err != nil {
t.Errorf("Unexpected error when deleting team name (%s)", err)
} else {
s.refresh()
if teamNames := s.TeamNames(); len(teamNames) != 1 {
t.Errorf("Expected to find 1 registered team, found %d (%s), instead", len(teamNames), teamNames)
}
}
// Try deleting a team that doesn't exist
s.refresh()
if err := s.DeleteTeamName(teamID1); err == nil {
t.Error("Expected to receive error when deleting team, received nil, instead")
}
// And looking up its team ID
s.refresh()
if teamID, err := s.TeamIDFromName(teamName1); err == nil {
t.Error("Expected to receive error, received nil, instead")
} else {
if teamID != "" {
t.Errorf("Expected to retrieve empty team ID, received (%s), instead", teamID)
}
}
s.refresh()
if teamNames := s.TeamNames(); len(teamNames) != 1 {
t.Errorf("Expected to find 1 registered team, found %d (%s), instead", len(teamNames), teamNames)
}
}
// Test what happens to team name functionality when we have various missing data
func TestStateTeamNameNoTeamIDs(t *testing.T) {
s := NewTestState()
s.refresh()
teamID1 := "foo"
teamName1 := "bar"
s.Fs.Remove("teamids.txt")
if err := s.SetTeamName(teamID1, teamName1); err == nil {
t.Error("Expected to receive error when adding team name when teamids.txt doesn't exist, received nil, instead")
}
}
func TestStateUpdateCachesNoPoints(t *testing.T) {
s := NewTestState()
s.refresh()
// Just exercise this
s.Fs.Remove("points.log")
s.updateCaches()
}
func TestStateUpdateCachesNoTeamsDir(t *testing.T) {
s := NewTestState()
s.refresh()
// Just exercise this
s.Fs.Remove("teams")
s.updateCaches()
} }
func TestDevelState(t *testing.T) { func TestDevelState(t *testing.T) {