Skip to content

Commit

Permalink
Fix port flag
Browse files Browse the repository at this point in the history
  • Loading branch information
caalberts committed Jul 19, 2018
1 parent 8907f72 commit 4155eef
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
6 changes: 3 additions & 3 deletions cmd/localroast/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"github.com/caalberts/localroast/json"
)

var port = flag.String("port", "8080", "port number")

func main() {
port := flag.String("port", "8080", "port number")

flag.Parse()
args := flag.Args()

err := json.NewCommand().Execute(args)
err := json.NewCommand().Execute(*port, args)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions json/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewCommand() Command {
}

// Execute runs the command and start a server.
func (c Command) Execute(args []string) error {
func (c Command) Execute(port string, args []string) error {
bytes, err := c.r.Read(args)
if err != nil {
return err
Expand All @@ -43,7 +43,7 @@ func (c Command) Execute(args []string) error {
return err
}

server := c.s("8080", schema)
server := c.s(port, schema)

return server.ListenAndServe()
}
11 changes: 8 additions & 3 deletions json/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ func (m *mockServer) ListenAndServe() error {
return args.Error(0)
}

var port = "8080"

func TestExecuteJSONCommand(t *testing.T) {
r := new(mockReader)
p := new(mockParser)
s := new(mockServer)

var parsedSchema []localroast.Schema
var serverPort string
sFunc := func(port string, schemas []localroast.Schema) http.Server {
parsedSchema = schemas
serverPort = port
return s
}

Expand All @@ -63,10 +67,11 @@ func TestExecuteJSONCommand(t *testing.T) {
s.On("ListenAndServe").Return(nil)

cmd := Command{r, p, sFunc}
err := cmd.Execute(args)
err := cmd.Execute(port, args)

assert.Nil(t, err)
assert.Equal(t, mockSchema, parsedSchema)
assert.Equal(t, port, serverPort)

r.AssertExpectations(t)
p.AssertExpectations(t)
Expand All @@ -86,7 +91,7 @@ func TestReadError(t *testing.T) {
r.On("Read", args).Return([]byte(""), errors.New(errorMsg))

cmd := Command{r, p, sFunc}
err := cmd.Execute(args)
err := cmd.Execute(port, args)

assert.NotNil(t, err)
assert.Equal(t, "Failed to read file", err.Error())
Expand All @@ -111,7 +116,7 @@ func TestParseError(t *testing.T) {
p.On("Parse", mockResult).Return([]localroast.Schema{}, errors.New(errorMsg))

cmd := Command{r, p, sFunc}
err := cmd.Execute(args)
err := cmd.Execute(port, args)

assert.NotNil(t, err)
assert.Equal(t, errorMsg, err.Error())
Expand Down

0 comments on commit 4155eef

Please sign in to comment.