This commit is contained in:
Neale Pickett 2019-12-01 20:53:13 -07:00
parent 5dccc6431d
commit 97da227777
2 changed files with 26 additions and 26 deletions

View File

@ -116,20 +116,20 @@ func (s *State) TeamName(teamId string) (string, error) {
// Write out team name. This can only be done once. // Write out team name. This can only be done once.
func (s *State) SetTeamName(teamId string, teamName string) error { func (s *State) SetTeamName(teamId string, teamName string) error {
if f, err := s.fs.Open("teamids.txt"); err != nil { if f, err := s.fs.Open("teamids.txt"); err != nil {
return fmt.Errorf("Team IDs file does not exist") return fmt.Errorf("Team IDs file does not exist")
} else { } else {
ok := false found := false
scanner := bufio.NewScanner(f) scanner := bufio.NewScanner(f)
for scanner.Scan() { for scanner.Scan() {
if scanner.Text() == teamId { if scanner.Text() == teamId {
ok = true found = true
break break
} }
} }
f.Close() f.Close()
if !ok { if !found {
return fmt.Errorf("Team ID not found in list of valid Team IDs") return fmt.Errorf("Team ID not found in list of valid Team IDs")
} }
} }
teamFile := filepath.Join("teams", teamId) teamFile := filepath.Join("teams", teamId)

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"bytes" "bytes"
"github.com/spf13/afero" "github.com/spf13/afero"
"os" "os"
"testing" "testing"
@ -20,10 +20,10 @@ func TestState(t *testing.T) {
s := NewState(fs) s := NewState(fs)
s.Cleanup() s.Cleanup()
pl := s.PointsLog() pl := s.PointsLog()
if len(pl) != 0 { if len(pl) != 0 {
t.Errorf("Empty points log is not empty") t.Errorf("Empty points log is not empty")
} }
mustExist("initialized") mustExist("initialized")
mustExist("enabled") mustExist("enabled")
@ -31,20 +31,20 @@ func TestState(t *testing.T) {
teamidsBuf, err := afero.ReadFile(fs, "teamids.txt") teamidsBuf, err := afero.ReadFile(fs, "teamids.txt")
if err != nil { if err != nil {
t.Errorf("Reading teamids.txt: %v", err) t.Errorf("Reading teamids.txt: %v", err)
} }
teamids := bytes.Split(teamidsBuf, []byte("\n")) teamids := bytes.Split(teamidsBuf, []byte("\n"))
if (len(teamids) != 101) || (len(teamids[100]) > 0) { if (len(teamids) != 101) || (len(teamids[100]) > 0) {
t.Errorf("There weren't 100 teamids, there were %d", len(teamids)) t.Errorf("There weren't 100 teamids, there were %d", len(teamids))
} }
myTeam := string(teamids[0]) myTeam := string(teamids[0])
if err := s.SetTeamName("bad team ID", "bad team name"); err == nil { if err := s.SetTeamName("bad team ID", "bad team name"); err == nil {
t.Errorf("Setting bad team ID didn't raise an error") t.Errorf("Setting bad team ID didn't raise an error")
} }
if err := s.SetTeamName(myTeam, "My Team"); err != nil { if err := s.SetTeamName(myTeam, "My Team"); err != nil {
t.Errorf("Setting team name: %v", err) t.Errorf("Setting team name: %v", err)
} }
} }