Command line and project structure

    Our product owner now wants to pivot by introducing a second application - a command line application.

    For now, it will just need to be able to record a player’s win when the user types Ruth wins. The intention is to eventually be a tool for helping users play poker.

    The product owner wants the database to be shared amongst the two applications so that the league updates according to wins recorded in the new application.

    We have an application with a main.go file that launches an HTTP server. The HTTP server won’t be interesting to us for this exercise but the abstraction it uses will. It depends on a PlayerStore.

    In the previous chapter, we made a FileSystemPlayerStore which implements that interface. We should be able to re-use some of this for our new application.

    Some project refactoring first

    Our project now needs to create two binaries, our existing web server and the command line app.

    Before we get stuck into our new work we should structure our project to accommodate this.

    So far all the code has lived in one folder, in a path looking like this

    $GOPATH/src/github.com/your-name/my-app

    In order for you to make an application in Go, you need a main function inside a package main. So far all of our “domain” code has lived inside package main and our func main can reference everything.

    This was fine so far and it is good practice not to go over-the-top with package structure. If you take the time to look through the standard library you will see very little in the way of lots of folders and structure.

    Thankfully it’s pretty straightforward to add structure when you need it.

    Inside the existing project create a cmd directory with a webserver directory inside that (e.g mkdir -p cmd/webserver).

    Move the main.go inside there.

    If you have tree installed you should run it and your structure should look like this

    1. .
    2. ├── FileSystemStore.go
    3. ├── FileSystemStore_test.go
    4. ├── cmd
    5. └── webserver
    6. └── main.go
    7. ├── league.go
    8. ├── server.go
    9. ├── server_integration_test.go
    10. ├── server_test.go
    11. ├── tape.go
    12. └── tape_test.go

    We now effectively have a separation between our application and the library code but we now need to change some package names. Remember when you build a Go application its package must be main.

    Change all the other code to have a package called poker.

    Finally, we need to import this package into main.go so we can use it to create our web server. Then we can use our library code by using poker.FunctionName.

    The paths will be different on your computer, but it should be similar to this:

    1. package main
    2. import (
    3. "log"
    4. "net/http"
    5. "os"
    6. "github.com/quii/learn-go-with-tests/command-line/v1"
    7. )
    8. const dbFileName = "game.db.json"
    9. func main() {
    10. db, err := os.OpenFile(dbFileName, os.O_RDWR|os.O_CREATE, 0666)
    11. if err != nil {
    12. log.Fatalf("problem opening %s %v", dbFileName, err)
    13. }
    14. store, err := poker.NewFileSystemPlayerStore(db)
    15. if err != nil {
    16. log.Fatalf("problem creating file system player store, %v ", err)
    17. }
    18. server := poker.NewPlayerServer(store)
    19. if err := http.ListenAndServe(":5000", server); err != nil {
    20. log.Fatalf("could not listen on port 5000 %v", err)
    21. }
    22. }

    The full path may seem a bit jarring, but this is how you can import any publicly available library into your code.

    By separating our domain code into a separate package and committing it to a public repo like GitHub any Go developer can write their own code which imports that package the features we’ve written available. The first time you try and run it will complain it is not existing but all you need to do is run go get.

    .

    • Inside the root run go test and check they’re still passing
    • Go inside our cmd/webserver and do go run main.go
      • Visit http://localhost:5000/league and you should see it’s still working

    Walking skeleton

    Before we get stuck into writing tests, let’s add a new application that our project will build. Create another directory inside cmd called cli (command line interface) and add a main.go with the following

    1. package main
    2. import "fmt"
    3. func main() {
    4. fmt.Println("Let's play poker")
    5. }

    The first requirement we’ll tackle is recording a win when the user types {PlayerName} wins.

    Write the test first

    We know we need to make something called CLI which will allow us to Play poker. It’ll need to read user input and then record wins to a PlayerStore.

    Before we jump too far ahead though, let’s just write a test to check it integrates with the PlayerStore how we’d like.

    1. func TestCLI(t *testing.T) {
    2. playerStore := &StubPlayerStore{}
    3. cli := &CLI{playerStore}
    4. cli.PlayPoker()
    5. if len(playerStore.winCalls) !=1 {
    6. t.Fatal("expected a win call but didn't get any")
    7. }
    8. }
    • We can use our StubPlayerStore from other tests
    • We pass in our dependency into our not yet existing CLI type
    • Trigger the game by an unwritten PlayPoker method
    • Check that a win is recorded

    Try to run the test

    1. # github.com/quii/learn-go-with-tests/command-line/v2
    2. ./cli_test.go:25:10: undefined: CLI

    Write the minimal amount of code for the test to run and check the failing test output

    At this point, you should be comfortable enough to create our new CLI struct with the respective field for our dependency and add a method.

    You should end up with code like this

    1. type CLI struct {
    2. playerStore PlayerStore
    3. }
    4. func (cli *CLI) PlayPoker() {}

    Remember we’re just trying to get the test running so we can check the test fails how we’d hope

    1. --- FAIL: TestCLI (0.00s)
    2. cli_test.go:30: expected a win call but didn't get any
    3. FAIL
    1. func (cli *CLI) PlayPoker() {
    2. cli.playerStore.RecordWin("Cleo")
    3. }

    That should make it pass.

    Next, we need to simulate reading from Stdin (the input from the user) so that we can record wins for specific players.

    Let’s extend our test to exercise this.

    Write the test first

    1. func TestCLI(t *testing.T) {
    2. in := strings.NewReader("Chris wins\n")
    3. playerStore := &StubPlayerStore{}
    4. cli := &CLI{playerStore, in}
    5. cli.PlayPoker()
    6. if len(playerStore.winCalls) < 1 {
    7. t.Fatal("expected a win call but didn't get any")
    8. }
    9. got := playerStore.winCalls[0]
    10. want := "Chris"
    11. if got != want {
    12. t.Errorf("didn't record correct winner, got %q, want %q", got, want)
    13. }
    14. }

    os.Stdin is what we’ll use in main to capture the user’s input. It is a *File under the hood which means it implements io.Reader which as we know by now is a handy way of capturing text.

    We create an io.Reader in our test using the handy strings.NewReader, filling it with what we expect the user to type.

    Try to run the test

    ./CLI_test.go:12:32: too many values in struct initializer

    Write the minimal amount of code for the test to run and check the failing test output

    We need to add our new dependency into CLI.

    Write enough code to make it pass

    1. --- FAIL: TestCLI (0.00s)
    2. CLI_test.go:23: didn't record the correct winner, got 'Cleo', want 'Chris'
    3. FAIL

    Remember to do the strictly easiest thing first

    1. func (cli *CLI) PlayPoker() {
    2. cli.playerStore.RecordWin("Chris")

    The test passes. We’ll add another test to force us to write some real code next, but first, let’s refactor.

    In server_test we earlier did checks to see if wins are recorded as we have here. Let’s DRY that assertion up into a helper

    1. func assertPlayerWin(t *testing.T, store *StubPlayerStore, winner string) {
    2. t.Helper()
    3. t.Fatalf("got %d calls to RecordWin want %d", len(store.winCalls), 1)
    4. }
    5. if store.winCalls[0] != winner {
    6. t.Errorf("did not store correct winner got %q want %q", store.winCalls[0], winner)
    7. }
    8. }

    Now replace the assertions in both server_test.go and CLI_test.go.

    The test should now read like so

    1. func TestCLI(t *testing.T) {
    2. in := strings.NewReader("Chris wins\n")
    3. playerStore := &StubPlayerStore{}
    4. cli := &CLI{playerStore, in}
    5. cli.PlayPoker()
    6. assertPlayerWin(t, playerStore, "Chris")
    7. }

    Now let’s write another test with different user input to force us into actually reading it.

    Write the test first

    1. func TestCLI(t *testing.T) {
    2. t.Run("record chris win from user input", func(t *testing.T) {
    3. in := strings.NewReader("Chris wins\n")
    4. playerStore := &StubPlayerStore{}
    5. cli := &CLI{playerStore, in}
    6. cli.PlayPoker()
    7. assertPlayerWin(t, playerStore, "Chris")
    8. })
    9. t.Run("record cleo win from user input", func(t *testing.T) {
    10. in := strings.NewReader("Cleo wins\n")
    11. playerStore := &StubPlayerStore{}
    12. cli := &CLI{playerStore, in}
    13. cli.PlayPoker()
    14. assertPlayerWin(t, playerStore, "Cleo")
    15. })
    16. }

    Try to run the test

    1. === RUN TestCLI
    2. --- FAIL: TestCLI (0.00s)
    3. === RUN TestCLI/record_chris_win_from_user_input
    4. --- PASS: TestCLI/record_chris_win_from_user_input (0.00s)
    5. === RUN TestCLI/record_cleo_win_from_user_input
    6. --- FAIL: TestCLI/record_cleo_win_from_user_input (0.00s)
    7. CLI_test.go:27: did not store correct winner got 'Chris' want 'Cleo'
    8. FAIL

    Write enough code to make it pass

    We’ll use a to read the input from the io.Reader.

    Update the code to the following

    1. type CLI struct {
    2. playerStore PlayerStore
    3. in io.Reader
    4. }
    5. func (cli *CLI) PlayPoker() {
    6. reader := bufio.NewScanner(cli.in)
    7. reader.Scan()
    8. cli.playerStore.RecordWin(extractWinner(reader.Text()))
    9. }
    10. func extractWinner(userInput string) string {
    11. return strings.Replace(userInput, " wins", "", 1)
    12. }

    The tests will now pass.

    • Scanner.Scan() will read up to a newline.
    • We then use Scanner.Text() to return the string the scanner read to.

    Now that we have some passing tests, we should wire this up into main. Remember we should always strive to have fully-integrated working software as quickly as we can.

    In main.go add the following and run it. (you may have to adjust the path of the second dependency to match what’s on your computer)

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/quii/learn-go-with-tests/command-line/v3"
    5. "log"
    6. "os"
    7. )
    8. const dbFileName = "game.db.json"
    9. func main() {
    10. fmt.Println("Let's play poker")
    11. fmt.Println("Type {Name} wins to record a win")
    12. db, err := os.OpenFile(dbFileName, os.O_RDWR|os.O_CREATE, 0666)
    13. if err != nil {
    14. log.Fatalf("problem opening %s %v", dbFileName, err)
    15. }
    16. store, err := poker.NewFileSystemPlayerStore(db)
    17. if err != nil {
    18. log.Fatalf("problem creating file system player store, %v ", err)
    19. }
    20. game := poker.CLI{store, os.Stdin}
    21. game.PlayPoker()
    22. }

    You should get an error

    1. command-line/v3/cmd/cli/main.go:32:25: implicit assignment of unexported field 'playerStore' in poker.CLI literal
    2. command-line/v3/cmd/cli/main.go:32:34: implicit assignment of unexported field 'in' in poker.CLI literal

    What’s happening here is because we are trying to assign to the fields playerStore and in in CLI. These are unexported (private) fields. We could do this in our test code because our test is in the same package as CLI (poker). But our main is in package main so it does not have access.

    This highlights the importance of integrating your work. We rightfully made the dependencies of our CLI private (because we don’t want them exposed to users of CLIs) but haven’t made a way for users to construct it.

    Is there a way to have caught this problem earlier?

    This is fine and it means on the odd occasion where we want to test something internal to the package we have access to the unexported types.

    But given we have advocated for not testing internal things generally, can Go help enforce that? What if we could test our code where we only have access to the exported types (like our main does)?

    When you’re writing a project with multiple packages I would strongly recommend that your test package name has _test at the end. When you do this you will only be able to have access to the public types in your package. This would help with this specific case but also helps enforce the discipline of only testing public APIs. If you still wish to test internals you can make a separate test with the package you want to test.

    An adage with TDD is that if you cannot test your code then it is probably hard for users of your code to integrate with it. Using package foo_test will help with this by forcing you to test your code as if you are importing it like users of your package will.

    Before fixing main let’s change the package of our test inside CLI_test.go to poker_test.

    If you have a well-configured IDE you will suddenly see a lot of red! If you run the compiler you’ll get the following errors

    We have now stumbled into more questions on package design. In order to test our software we made unexported stubs and helper functions which are no longer available for us to use in our CLI_test because the helpers are defined in the _test.go files in the poker package.

    Do we want to have our stubs and helpers ‘public’?

    This is a subjective discussion. One could argue that you do not want to pollute your package’s API with code to facilitate tests.

    In the presentation by Mitchell Hashimoto, it is described how at HashiCorp they advocate doing this so that users of the package can write tests without having to re-invent the wheel writing stubs. In our case, this would mean anyone using our poker package won’t have to create their own stub PlayerStore if they wish to work with our code.

    Anecdotally I have used this technique in other shared packages and it has proved extremely useful in terms of users saving time when integrating with our packages.

    So let’s create a file called testing.go and add our stub and our helpers.

    1. package poker
    2. import "testing"
    3. type StubPlayerStore struct {
    4. scores map[string]int
    5. winCalls []string
    6. league []Player
    7. }
    8. func (s *StubPlayerStore) GetPlayerScore(name string) int {
    9. score := s.scores[name]
    10. return score
    11. }
    12. func (s *StubPlayerStore) RecordWin(name string) {
    13. s.winCalls = append(s.winCalls, name)
    14. }
    15. func (s *StubPlayerStore) GetLeague() League {
    16. return s.league
    17. func AssertPlayerWin(t *testing.T, store *StubPlayerStore, winner string) {
    18. t.Helper()
    19. if len(store.winCalls) != 1 {
    20. }
    21. if store.winCalls[0] != winner {
    22. t.Errorf("did not store correct winner got %q want %q", store.winCalls[0], winner)
    23. }
    24. }
    25. // todo for you - the rest of the helpers

    You’ll need to make the helpers public (remember exporting is done with a capital letter at the start) if you want them to be exposed to importers of our package.

    In our CLI test you’ll need to call the code as if you were using it within a different package.

    1. func TestCLI(t *testing.T) {
    2. t.Run("record chris win from user input", func(t *testing.T) {
    3. in := strings.NewReader("Chris wins\n")
    4. playerStore := &poker.StubPlayerStore{}
    5. cli := &poker.CLI{playerStore, in}
    6. cli.PlayPoker()
    7. poker.AssertPlayerWin(t, playerStore, "Chris")
    8. })
    9. t.Run("record cleo win from user input", func(t *testing.T) {
    10. in := strings.NewReader("Cleo wins\n")
    11. playerStore := &poker.StubPlayerStore{}
    12. cli := &poker.CLI{playerStore, in}
    13. cli.PlayPoker()
    14. poker.AssertPlayerWin(t, playerStore, "Cleo")
    15. })
    16. }

    You’ll now see we have the same problems as we had in main

    1. ./CLI_test.go:15:26: implicit assignment of unexported field 'playerStore' in poker.CLI literal
    2. ./CLI_test.go:15:39: implicit assignment of unexported field 'in' in poker.CLI literal
    3. ./CLI_test.go:25:26: implicit assignment of unexported field 'playerStore' in poker.CLI literal
    4. ./CLI_test.go:25:39: implicit assignment of unexported field 'in' in poker.CLI literal

    The easiest way to get around this is to make a constructor as we have for other types. We’ll also change CLI so it stores a bufio.Scanner instead of the reader as it’s now automatically wrapped at construction time.

    1. type CLI struct {
    2. playerStore PlayerStore
    3. in *bufio.Scanner
    4. }
    5. func NewCLI(store PlayerStore, in io.Reader) *CLI {
    6. return &CLI{
    7. playerStore: store,
    8. in: bufio.NewScanner(in),
    9. }
    10. }

    By doing this, we can then simplify and refactor our reading code

    1. func (cli *CLI) PlayPoker() {
    2. userInput := cli.readLine()
    3. cli.playerStore.RecordWin(extractWinner(userInput))
    4. }
    5. func extractWinner(userInput string) string {
    6. return strings.Replace(userInput, " wins", "", 1)
    7. }
    8. func (cli *CLI) readLine() string {
    9. cli.in.Scan()
    10. return cli.in.Text()
    11. }

    Change the test to use the constructor instead and we should be back to the tests passing.

    Finally, we can go back to our new main.go and use the constructor we just made

    1. game := poker.NewCLI(store, os.Stdin)

    Try and run it, type “Bob wins”.

    Refactor

    We have some repetition in our respective applications where we are opening a file and creating a FileSystemStore from its contents. This feels like a slight weakness in our package’s design so we should make a function in it to encapsulate opening a file from a path and returning you the PlayerStore.

    1. func FileSystemPlayerStoreFromFile(path string) (*FileSystemPlayerStore, func(), error) {
    2. db, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
    3. if err != nil {
    4. return nil, nil, fmt.Errorf("problem opening %s %v", path, err)
    5. }
    6. closeFunc := func() {
    7. db.Close()
    8. }
    9. store, err := NewFileSystemPlayerStore(db)
    10. if err != nil {
    11. return nil, nil, fmt.Errorf("problem creating file system player store, %v ", err)
    12. }
    13. return store, closeFunc, nil
    14. }

    Now refactor both of our applications to use this function to create the store.

    CLI application code

    1. package main
    2. import (
    3. "github.com/quii/learn-go-with-tests/command-line/v3"
    4. "log"
    5. "os"
    6. "fmt"
    7. )
    8. const dbFileName = "game.db.json"
    9. func main() {
    10. store, close, err := poker.FileSystemPlayerStoreFromFile(dbFileName)
    11. if err != nil {
    12. log.Fatal(err)
    13. }
    14. defer close()
    15. fmt.Println("Let's play poker")
    16. fmt.Println("Type {Name} wins to record a win")
    17. poker.NewCLI(store, os.Stdin).PlayPoker()
    18. }

    Web server application code

    1. package main
    2. import (
    3. "github.com/quii/learn-go-with-tests/command-line/v3"
    4. "log"
    5. "net/http"
    6. )
    7. const dbFileName = "game.db.json"
    8. func main() {
    9. store, close, err := poker.FileSystemPlayerStoreFromFile(dbFileName)
    10. if err != nil {
    11. log.Fatal(err)
    12. }
    13. defer close()
    14. server := poker.NewPlayerServer(store)
    15. if err := http.ListenAndServe(":5000", server); err != nil {
    16. log.Fatalf("could not listen on port 5000 %v", err)
    17. }
    18. }

    Notice the symmetry: despite being different user interfaces the setup is almost identical. This feels like good validation of our design so far.And notice also that FileSystemPlayerStoreFromFile returns a closing function, so we can close the underlying file once we are done using the Store.

    Wrapping up

    This chapter meant we wanted to create two applications, re-using the domain code we’ve written so far. In order to do this, we needed to update our package structure so that we had separate folders for our respective mains.

    By doing this we ran into integration problems due to unexported values so this further demonstrates the value of working in small “slices” and integrating often.

    We learned how mypackage_test helps us create a testing environment which is the same experience for other packages integrating with your code, to help you catch integration problems and see how easy (or not!) your code is to work with.

    Reading user input

    We saw how reading from os.Stdin is very easy for us to work with as it implements io.Reader. We used bufio.Scanner to easily read line by line user input.