Skip to content

Commit

Permalink
Promote config.Config to an interface
Browse files Browse the repository at this point in the history
Sooo, we can mock it during command testing. When testing is hard, mock
it so you look good in front of others and don't care about all the
shit and potential bugs it can slip through. But hey, tests!
  • Loading branch information
gsamokovarov committed Mar 13, 2017
1 parent fca01f5 commit a71a832
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// CommandFn represents a command handler function.
type CommandFn func(Args, *config.Config)
type CommandFn func(Args, config.Config)

// Command represents a command line action.
type Command struct {
Expand Down
6 changes: 3 additions & 3 deletions cli/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import (
)

func TestDispatchCommand(t *testing.T) {
RegisterCommand("test", "A testing command.", func(Args, *config.Config) {})
RegisterCommand("test", "A testing command.", func(Args, config.Config) {})

args := Args([]string{"test"})
if _, err := DispatchCommand(args, "default"); err == nil {
t.Errorf("Expected an error on missing registered default command")
}

RegisterCommand("default", "A testing command.", func(Args, *config.Config) {})
RegisterCommand("default", "A testing command.", func(Args, config.Config) {})

if command, _ := DispatchCommand(args, "default"); command.Name != "test" {
t.Errorf("Expected test command to be dispatched and executed")
}
}

func TestCommandIsOption(t *testing.T) {
command := &Command{"--test", "Testing command.", func(Args, *config.Config) {}}
command := &Command{"--test", "Testing command.", func(Args, config.Config) {}}

if !command.IsOption() {
t.Errorf("Expected --test command to be an option")
Expand Down
2 changes: 1 addition & 1 deletion cmd/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
const proximity = 5
const osSeparator = string(os.PathSeparator)

func cdCmd(args cli.Args, conf *config.Config) {
func cdCmd(args cli.Args, conf config.Config) {
term := args.CommandName()
entries, err := conf.ReadEntries()

Expand Down
2 changes: 1 addition & 1 deletion cmd/chdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/gsamokovarov/jump/scoring"
)

func updateCmd(args cli.Args, conf *config.Config) {
func updateCmd(args cli.Args, conf config.Config) {
dir, err := os.Getwd()
if len(args) == 0 && err != nil {
cli.Exitf(1, "err: %s\n", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/gsamokovarov/jump/scoring"
)

func cleanCmd(args cli.Args, conf *config.Config) {
func cleanCmd(args cli.Args, conf config.Config) {
entries, err := conf.ReadEntries()
if err != nil {
cli.Exitf(1, "%s\n", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Options:{{range .Options}}
{{printf "%-12s %s" .Name .Desc}}{{end}}
`

func helpCmd(cli.Args, *config.Config) {
func helpCmd(cli.Args, config.Config) {
tmpl := template.Must(template.New("--help").Parse(helpUsage))
tmpl.Execute(os.Stderr, cli.Registry)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/gsamokovarov/jump/scoring"
)

func hintCmd(args cli.Args, conf *config.Config) {
func hintCmd(args cli.Args, conf config.Config) {
var hints scoring.Entries

term := args.CommandName()
Expand Down
2 changes: 1 addition & 1 deletion cmd/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var pinUsageMsg = `jump pin term [directory]
No term specified. See the signature of the pin call above.
`

func pinCmd(args cli.Args, conf *config.Config) {
func pinCmd(args cli.Args, conf config.Config) {
var err error

term := args.CommandName()
Expand Down
2 changes: 1 addition & 1 deletion cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/gsamokovarov/jump/shell"
)

func shellCmd(args cli.Args, _ *config.Config) {
func shellCmd(args cli.Args, _ config.Config) {
hint := args.CommandName()
if len(hint) == 0 {
hint = os.Getenv("SHELL")
Expand Down
2 changes: 1 addition & 1 deletion cmd/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/gsamokovarov/jump/config"
)

func topCmd(args cli.Args, conf *config.Config) {
func topCmd(args cli.Args, conf config.Config) {
entries, err := conf.ReadEntries()
if err != nil {
cli.Exitf(1, "%s\n", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

const version = "0.11.0"

func versionCmd(cli.Args, *config.Config) {
func versionCmd(cli.Args, config.Config) {
cli.Outf("%s\n", version)
}

Expand Down
21 changes: 17 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"
"os/user"
"path/filepath"

"github.com/gsamokovarov/jump/scoring"
)

const (
Expand All @@ -15,7 +17,18 @@ const (

// Config represents the config directory and all the miscellaneous
// configuration files we can have in there.
type Config struct {
type Config interface {
ReadEntries() (*scoring.Entries, error)
WriteEntries(*scoring.Entries) error

ReadSearch() Search
WriteSearch(string, int) error

FindPin(string) (string, bool)
WritePin(string, string) error
}

type fileConfig struct {
Dir string
Scores string
Search string
Expand All @@ -26,7 +39,7 @@ type Config struct {
//
// If the directories don't already exists, they are created and if the score
// file is present, it is loaded.
func Setup(dir string) (*Config, error) {
func Setup(dir string) (Config, error) {
// We get the directory check for free form os.MkdirAll.
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
Expand All @@ -36,13 +49,13 @@ func Setup(dir string) (*Config, error) {
search := filepath.Join(dir, defaultSearchFile)
pins := filepath.Join(dir, defaultPinsFile)

return &Config{dir, scores, search, pins}, nil
return &fileConfig{dir, scores, search, pins}, nil
}

// SetupDefault setups the config folder from a directory path.
//
// If the directory path is an empty string, the path is automatically guessed.
func SetupDefault(dir string) (*Config, error) {
func SetupDefault(dir string) (Config, error) {
dir, err := normalizeDir(dir)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions config/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// ReadEntries returns the current entries for the config.
//
// If the scores file is empty, the returned entries are empty.
func (c *Config) ReadEntries() (*scoring.Entries, error) {
func (c *fileConfig) ReadEntries() (*scoring.Entries, error) {
var entries scoring.Entries

scoresFile, err := createOrOpenLockedFile(c.Scores)
Expand All @@ -28,7 +28,7 @@ func (c *Config) ReadEntries() (*scoring.Entries, error) {
// WriteEntries the input scoring entries to a file.
//
// Sorts the entries before writing them to disk.
func (c *Config) WriteEntries(entries *scoring.Entries) error {
func (c *fileConfig) WriteEntries(entries *scoring.Entries) error {
scoresFile, err := createOrOpenLockedFile(c.Scores)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions config/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// FindPin tries to the directory from a pinned search term.
//
// If no search pinned search term is found.
func (c *Config) FindPin(term string) (dir string, found bool) {
func (c *fileConfig) FindPin(term string) (dir string, found bool) {
pinsFile, err := createOrOpenLockedFile(c.Pins)
if err != nil {
return
Expand All @@ -24,7 +24,7 @@ func (c *Config) FindPin(term string) (dir string, found bool) {
}

// WritePin saves a pinned search term into a file.
func (c *Config) WritePin(pin, value string) error {
func (c *fileConfig) WritePin(pin, value string) error {
pinsFile, err := createOrOpenLockedFile(c.Pins)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions config/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Search struct {
// ReadSearch reads the last saved search entry.
//
// If the last search doesn't exist, a zero value Search is returned.
func (c *Config) ReadSearch() (search Search) {
func (c *fileConfig) ReadSearch() (search Search) {
searchFile, err := createOrOpenLockedFile(c.Search)
if err != nil {
return
Expand All @@ -28,7 +28,7 @@ func (c *Config) ReadSearch() (search Search) {
}

// WriteSearch writes the last search entry to the current search entry.
func (c *Config) WriteSearch(term string, index int) error {
func (c *fileConfig) WriteSearch(term string, index int) error {
searchFile, err := createOrOpenLockedFile(c.Search)
if err != nil {
return err
Expand Down

0 comments on commit a71a832

Please sign in to comment.