Skip to content

Commit

Permalink
Add TestScript tests, use io instead of ioutil, fix unchecked error,
Browse files Browse the repository at this point in the history
bump Go to 1.21
  • Loading branch information
ivanfetch committed Aug 19, 2023
1 parent c96419d commit 9d0c409
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 13 deletions.
6 changes: 3 additions & 3 deletions chatserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"os"
"strings"
Expand Down Expand Up @@ -74,9 +74,9 @@ func TestChatSession(t *testing.T) {
var err error
var server *chat.Server
if testing.Verbose() {
server, err = chat.NewServer(chat.WithDebugLogging())
server, err = chat.NewServer(chat.WithDebugLogging(), chat.WithListenAddress(":8888"))
} else {
server, err = chat.NewServer(chat.WithLogWriter(ioutil.Discard)) // discard non-debug status output
server, err = chat.NewServer(chat.WithLogWriter(io.Discard), chat.WithListenAddress(":8888")) // discard non-debug status output
}
if err != nil {
t.Fatal(err)
Expand Down
33 changes: 27 additions & 6 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,36 @@ import (
flag "github.com/spf13/pflag"
)

// RunCLI processes command-line arguments, instantiates a new chat server, calls ListenAndServe, then
// waits for the chat server routines to cleanup and exit.
func RunCLI() int {
// RunCLI processes command-line arguments, instantiates a new chat server,
// calls ListenAndServe, then optionally waits for chat server routines to exit.
func processCLIArgsAndRunServer(waitForExit bool) int {
server, err := NewServerFromArgs(os.Args[1:])
if err != nil {
fmt.Println(err)
return 1
}
server.ListenAndServe()
server.WaitForExit()
if waitForExit {
server.WaitForExit()
}
return 0
}

// RunCLI processes command-line arguments, instantiates a new chat server, calls ListenAndServe, then
// waits for the chat server routines to cleanup and exit.
func RunCLI() int {
return processCLIArgsAndRunServer(true)
}

// RunCLIWithoutWaitingForExit processes command-line arguments, instantiates
// a new chat server, calls ListenAndServe, but does not wait for the chat
// server routines to exit.
// This is useful for Go TestScript tests, which can avoid retaining and
// calling cleanup methods on the chat server.
func RunCLIWithoutWaitingForExit() int {
return processCLIArgsAndRunServer(false)
}

// NewServerFromArgs returns a type *Server after processing command-line
// arguments.
func NewServerFromArgs(args []string) (*Server, error) {
Expand Down Expand Up @@ -50,7 +67,7 @@ The listen address can also be set by setting the CHATSERVER_LISTEN_ADDRESS envi
}
fs.VisitAll(setCLIFlagFromEnvVar)
if *CLIVersion {
return nil, fmt.Errorf("version %s, git commit %s\n", Version, GitCommit)
return nil, fmt.Errorf("version %s, git commit %s", Version, GitCommit)
}
var optionalConfig []ServerOption
if *CLIDebugLogging {
Expand All @@ -77,6 +94,10 @@ func setCLIFlagFromEnvVar(f *flag.Flag) {
envVarName := "CHATSERVER_" + strings.ToUpper(strings.ReplaceAll(f.Name, "-", "_"))
envVarValue := os.Getenv(envVarName)
if envVarValue != "" && f.Value.String() == f.DefValue {
_ = f.Value.Set(envVarValue)
err := f.Value.Set(envVarValue)
if err != nil {
// The error is not returned because FlagSet.VisitAll will not accept it.
panic(fmt.Errorf("while setting value %q to flag %v: %w", envVarValue, f, err))
}
}
}
12 changes: 8 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
module github.com/ivanfetch/chatserver

go 1.20
go 1.21

require github.com/sirupsen/logrus v1.9.2
require (
github.com/rogpeppe/go-internal v1.11.0
github.com/sirupsen/logrus v1.9.2
github.com/spf13/pflag v1.0.5
)

require (
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/tools v0.1.12 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y=
github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand All @@ -12,6 +14,10 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
23 changes: 23 additions & 0 deletions script_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package chat_test

// Note: The go test -testwork flag preserves the TestScript temporary directory.

import (
"os"
"testing"

chat "github.com/ivanfetch/chatserver"
"github.com/rogpeppe/go-internal/testscript"
)

func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"chatserver": chat.RunCLIWithoutWaitingForExit,
}))
}

func TestScript(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/script",
})
}
2 changes: 2 additions & 0 deletions testdata/script/debug.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
chatserver --debug-logging
stderr 'level=debug'
4 changes: 4 additions & 0 deletions testdata/script/help.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
! chatserver --help
stdout 'Use a program like telnet, or nc'
stderr 'Enable debug logging'
stderr 'The TCP address the chat server should listen on,'
3 changes: 3 additions & 0 deletions testdata/script/listen-address-envvar.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
env CHATSERVER_LISTEN_ADDRESS=':8765'
exec chatserver
stderr 'listening for connections on :8765'
2 changes: 2 additions & 0 deletions testdata/script/listen-address.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exec chatserver --listen-address :5678
stderr 'listening for connections on :5678'
2 changes: 2 additions & 0 deletions testdata/script/version.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
! exec chatserver --version
stdout 'version development, git commit unknown'

0 comments on commit 9d0c409

Please sign in to comment.