Skip to content

Commit

Permalink
Revert "#29 異なるパッケージを監視対象にする処理を追加"
Browse files Browse the repository at this point in the history
  • Loading branch information
meian authored Apr 17, 2020
1 parent fee1440 commit e0f4577
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 60 deletions.
1 change: 0 additions & 1 deletion cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func testAction(c *cli.Context) error {

terminal.Clear()
config.Show()
fmt.Println("========================================")
fmt.Println("watch directories:", nc.Directories)

go test.LoopFSEvent(nc)
Expand Down
57 changes: 24 additions & 33 deletions cmd/test/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

// Config はWatcherの設定を保持する
type Config struct {
// Dirs は監視対象のディレクトリパス一覧
Dirs []string
// Dir は監視対象のディレクトリパス
Dir string
// Args は go test に渡す引数
Args []string
// Recursive はサブディレクトリを監視するかどうか
Expand All @@ -21,45 +21,36 @@ type Config struct {

// NewConfig は設定情報を生成する
func NewConfig(c *cli.Context) (*Config, error) {
dirs, args := parseArgs(c.Args().Slice())
correctDirs(dirs)
for _, arg := range args {
if arg == "-run" {
return nil, errors.New("cannot use -run")
}
}
return &Config{
Dirs: dirs,
Args: args,
config := &Config{
Args: []string{},
Recursive: c.Bool("recursive"),
}, nil
}

func parseArgs(slice []string) (dirs []string, args []string) {
dirs = []string{"."}
if len(slice) == 0 {
return dirs, []string{}
}
for i, arg := range slice {
if strings.HasPrefix(arg, "-") {
dirs, slice = slice[0:i], slice[i:]
break
}
args := c.Args().Slice()
if len(args) == 0 {
config.Dir = "."
return config, nil
}
if len(slice) > 0 && slice[0] == "--" {
slice = slice[1:]
if !strings.HasPrefix(args[0], "-") {
config.Dir = strings.TrimRight(filepath.ToSlash(args[0]), "/")
args = args[1:]
} else {
config.Dir = "."
}
return dirs, slice
}

func correctDirs(dirs []string) {
for i, dir := range dirs {
dirs[i] = strings.TrimRight(filepath.ToSlash(dir), "/")
if len(args) > 0 && args[0] == "--" {
args = args[1:]
}
for i := 0; i < len(args)-1; i++ {
if args[i] == "-run" {
return nil, errors.New("cannot use -run")
}
}
config.Args = args
return config, nil
}

// Show はコンフィグの情報を表示する
func (c *Config) Show() {
fmt.Printf("directory: %v (recursive: %v)\n", c.Dirs, c.Recursive)
fmt.Println("directory:", c.Dir)
fmt.Println("recursive:", c.Recursive)
fmt.Println("arguments:", c.Args)
}
37 changes: 12 additions & 25 deletions cmd/test/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const (

// Context はテスト実行時の情報を保持する
type Context struct {
Config *Config
Watcher *notify.Watcher
Args []string
Directories []string
Changed *file.PairMap
State int
Expand All @@ -27,33 +27,20 @@ type Context struct {

// NewContext はコンフィグから実行情報を生成する
func NewContext(config *Config) (*Context, error) {
dirs, err := collectDirs(config)
if err != nil {
return nil, err
c := &Context{
Config: config,
Changed: file.NewPairMap(),
State: None,
Done: make(chan error),
}
return &Context{
Changed: file.NewPairMap(),
State: None,
Done: make(chan error),
Args: config.Args,
Directories: dirs,
}, nil
}

func collectDirs(config *Config) ([]string, error) {
dirs := []string{}
dirMap := map[string]bool{}
for _, dir := range config.Dirs {
tdirs, err := file.TargetDirs(dir, config.Recursive)
if config.Recursive {
dirs, err := file.RecurseDir(config.Dir)
if err != nil {
return nil, err
}
for _, tdir := range tdirs {
if _, ok := dirMap[tdir]; !ok {
dirs = append(dirs, tdir)
dirMap[tdir] = true
}
}
c.Directories = dirs
} else {
c.Directories = []string{config.Dir}
}
return dirs, nil
return c, nil
}
2 changes: 1 addition & 1 deletion cmd/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func cmdArgs(c *Context, src string) ([]string, error) {
if err != nil {
return nil, err
}
args := append([]string{"test", path.DirPath(src), "-run", pattern}, c.Args...)
args := append([]string{"test", path.DirPath(src), "-run", pattern}, c.Config.Args...)
return args, nil
}

Expand Down

0 comments on commit e0f4577

Please sign in to comment.