Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janiltonmaciel committed Sep 3, 2020
1 parent 0fd57df commit fe56ee1
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 66 deletions.
30 changes: 2 additions & 28 deletions lib/cli.go → cmd/cli_template.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package lib

import (
"fmt"

"github.com/urfave/cli/v2"
)
package cmd

// AppHelpTemplate is the text template for the Default help topic.
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
// https://github.com/urfave/cli/blob/master/help.go
var AppHelpTemplate = `NAME:
var appHelpTemplate = `NAME:
{{.Name}}{{if .Usage}} - {{.Usage}}{{end}}
USAGE:
Expand Down Expand Up @@ -69,23 +63,3 @@ AUTHOR{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
{{end}}{{$author}}{{end}}
{{end}}
`

func VersionPrinter(commit, date string) func(c *cli.Context) {
return func(c *cli.Context) {
printLogo()
fmt.Fprintf(c.App.Writer, "version: %s\n", c.App.Version)
fmt.Fprintf(c.App.Writer, "commit: %s\n", commit)
fmt.Fprintf(c.App.Writer, "date: %s\n", date)
fmt.Fprintf(c.App.Writer, "author: %s\n", c.App.Authors[0].Name)
}
}

func printLogo() {
fmt.Println(`
_______.___________. ___ .___________. __ __ ___ _______.
/ | | / \ | || | | |/ / / |
| (----'---| |----' / ^ \ '---| |----'| | | ' / | (----'
\ \ | | / /_\ \ | | | | | < \ \
.----) | | | / _____ \ | | | | | . \ .----) |
|_______/ |__| /__/ \__\ |__| |__| |__|\__\ |_______/`)
}
24 changes: 16 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package cmd

import (
"os"
"fmt"

"github.com/janiltonmaciel/statiks/lib"
"github.com/urfave/cli/v2"
)

var author = "Janilton Maciel <janilton@gmail.com>"

func Execute(version, commit, date string) error {
cli.AppHelpTemplate = lib.AppHelpTemplate
cli.VersionPrinter = lib.VersionPrinter(commit, date)
// CreateApp.
func CreateApp(version, commit, date string) *cli.App {
cli.AppHelpTemplate = appHelpTemplate
cli.VersionPrinter = versionPrinter(commit, date)
cli.HelpFlag = &cli.BoolFlag{
Name: "help",
Usage: "show help",
Expand All @@ -25,15 +24,15 @@ func Execute(version, commit, date string) error {
app := createCliApp(
version,
)
return app.Run(os.Args)
return app
}

func createCliApp(version string) *cli.App {
app := cli.NewApp()
app.Name = "statiks"
app.Usage = "fast, zero-configuration, static HTTP filer server."
app.UsageText = "statiks [options] <path>"
app.Authors = []*cli.Author{{Name: author}}
app.Authors = []*cli.Author{{Name: "Janilton Maciel", Email: "janilton@gmail.com"}}
app.Version = version
app.Flags = createFlags()
app.Action = func(c *cli.Context) error {
Expand Down Expand Up @@ -110,3 +109,12 @@ func createFlags() []cli.Flag {
}
return flags
}

func versionPrinter(commit, date string) func(c *cli.Context) {
return func(c *cli.Context) {
fmt.Fprintf(c.App.Writer, "version: %s\n", c.App.Version)
fmt.Fprintf(c.App.Writer, "commit: %s\n", commit)
fmt.Fprintf(c.App.Writer, "date: %s\n", date)
fmt.Fprintf(c.App.Writer, "author: %s\n", c.App.Authors[0].Name)
}
}
30 changes: 30 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd_test

import (
"flag"

"github.com/janiltonmaciel/statiks/cmd"
"github.com/urfave/cli/v2"
check "gopkg.in/check.v1"
)

func (s *StatiksSuite) TestApp(c *check.C) {
commit := "da3c509"
date := "2020-09-03T14:45:36Z"
version := "v0.1"
app := cmd.CreateApp(version, commit, date)

c.Assert(app.Version, check.Equals, version)
c.Assert(app.Authors[0].Name, check.Equals, "Janilton Maciel")
c.Assert(app.Authors[0].Email, check.Equals, "janilton@gmail.com")
}

func (s *StatiksSuite) TestVersionPrinter(c *check.C) {
commit := "da3c509"
date := "2020-09-03T14:45:36Z"
version := "v0.1"
app := cmd.CreateApp(version, commit, date)
set := flag.NewFlagSet("test", 0)
ctx := cli.NewContext(app, set, nil)
cli.VersionPrinter(ctx)
}
21 changes: 21 additions & 0 deletions cmd/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd_test

import (
"testing"

check "gopkg.in/check.v1"
)

type StatiksSuite struct {
t *testing.T
}

var sSuite = &StatiksSuite{}

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) {
sSuite.t = t
check.TestingT(t)
}

var _ = check.Suite(sSuite)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ require (
github.com/rs/cors v1.7.0
github.com/urfave/cli/v2 v2.2.0
github.com/urfave/negroni v1.0.0
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a h1:i47hUS795cOydZI4AwJQCKXOr4BvxzvikwDoDtHhP2Y=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U=
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
Expand Down
23 changes: 0 additions & 23 deletions lib/cli_test.go

This file was deleted.

14 changes: 12 additions & 2 deletions lib/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,25 @@ func (s *Server) GetHandler() http.Handler {
}

func (s *Server) runHTTP() error {
printLogo()
s.log()
fmt.Printf("\nRunning on HTTP\n ⚡️ http://%s, serving '%s'\n\n", s.config.Address, s.config.Path)
fmt.Print("CTRL-C to stop the️ server\n")
return http.ListenAndServe(s.config.Address, s.handler)
}

func (s *Server) runHTTPS() error {
printLogo()
s.log()
fmt.Printf("\nRunning on HTTPS\n ⚡️ https://%s, serving '%s'\n\n", s.config.Address, s.config.Path)
fmt.Print("CTRL-C to stop the️ server\n")
return http.ListenAndServeTLS(s.config.Address, s.config.Cert, s.config.Key, s.handler)
}

func (s *Server) log() {
fmt.Println(`
_______.___________. ___ .___________. __ __ ___ _______.
/ | | / \ | || | | |/ / / |
| (----'---| |----' / ^ \ '---| |----'| | | ' / | (----'
\ \ | | / /_\ \ | | | | | < \ \
.----) | | | / _____ \ | | | | | . \ .----) |
|_______/ |__| /__/ \__\ |__| |__| |__|\__\ |_______/`)
}
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package main

import (
golog "log"
"log"
"os"

"github.com/janiltonmaciel/statiks/cmd"
)
Expand All @@ -13,7 +14,8 @@ var (
)

func main() {
if err := cmd.Execute(version, commit, date); err != nil {
golog.Fatal(err)
app := cmd.CreateApp(version, commit, date)
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}

0 comments on commit fe56ee1

Please sign in to comment.