diff --git a/run_test.go b/run_test.go index 855a5e5..751d08e 100644 --- a/run_test.go +++ b/run_test.go @@ -245,3 +245,33 @@ func bufErrorPipe(t *testing.T) (io.ReadCloser, func()) { os.Stderr = stderr } } + +func TestFeatureFilePathParser(t *testing.T) { + + type Case struct { + input string + path string + line int + } + + cases := []Case{ + {"/home/test.feature", "/home/test.feature", -1}, + {"/home/test.feature:21", "/home/test.feature", 21}, + {"test.feature", "test.feature", -1}, + {"test.feature:2", "test.feature", 2}, + {"", "", -1}, + {"/c:/home/test.feature", "/c:/home/test.feature", -1}, + {"/c:/home/test.feature:3", "/c:/home/test.feature", 3}, + {"D:\\home\\test.feature:3", "D:\\home\\test.feature", 3}, + } + + for i, c := range cases { + p, ln := extractFeaturePathLine(c.input) + if p != c.path { + t.Fatalf(`result path "%s" != "%s" at %d`, p, c.path, i) + } + if ln != c.line { + t.Fatalf(`result line "%d" != "%d" at %d`, ln, c.line, i) + } + } +} diff --git a/suite.go b/suite.go index cf80064..4230945 100644 --- a/suite.go +++ b/suite.go @@ -617,21 +617,27 @@ func (s *Suite) printStepDefinitions(w io.Writer) { } } +var pathLineRe = regexp.MustCompile(`:([\d]+)$`) + +func extractFeaturePathLine(p string) (string, int) { + line := -1 + retPath := p + if m := pathLineRe.FindStringSubmatch(p); len(m) > 0 { + if i, err := strconv.Atoi(m[1]); err == nil { + line = i + retPath = p[:strings.LastIndexByte(p, ':')] + } + } + return retPath, line +} + func parseFeatures(filter string, paths []string) ([]*feature, error) { byPath := make(map[string]*feature) var order int for _, pat := range paths { // check if line number is specified - parts := strings.Split(pat, ":") - path := parts[0] - line := -1 + path, line := extractFeaturePathLine(pat) var err error - if len(parts) > 1 { - line, err = strconv.Atoi(parts[1]) - if err != nil { - return nil, fmt.Errorf("line number should follow after colon path delimiter") - } - } // parse features err = filepath.Walk(path, func(p string, f os.FileInfo, err error) error { if err == nil && !f.IsDir() && strings.HasSuffix(p, ".feature") {