Skip to content

Commit

Permalink
Merge pull request #4 from skelouse/add-recursion-limit-to-discover
Browse files Browse the repository at this point in the history
Add recursion limit when searching for cover.out
  • Loading branch information
skelouse authored Mar 1, 2023
2 parents 6be2460 + 9bfd1db commit db9ab6a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ go 1.19
require (
github.com/urfave/cli/v3 v3.0.0-20230118155752-3e0609a0df87
golang.org/x/tools v0.5.0
gotest.tools/v3 v3.4.0
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)
28 changes: 28 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v3 v3.0.0-20230118155752-3e0609a0df87 h1:qluKKSuxJFfPFUJ0pPDPUMRlNBu4sa+wVOY6t3Idc4c=
github.com/urfave/cli/v3 v3.0.0-20230118155752-3e0609a0df87/go.mod h1:gHI/xEYplFhOa3Y90xJleh3kqqsSanBj/19hVFxiVZ4=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.5.0 h1:+bSpV5HIeWkuvgaMfI3UmKRThoTA5ODJTUd8T17NO+4=
golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=
gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g=
25 changes: 21 additions & 4 deletions parse/discover.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
package parse

import (
"errors"
"os"
"path/filepath"
)

const maxRecurse = 20

var recursionError = errors.New("max recursion depth reached")

// Discover will walk the current directory and sub-directories in search of `cover.out`
// Returns cover.out or an error if not found
func Discover() (coverFilePath string, err error) {
return coverPath(".")
return coverPath(".", 0)
}

func coverPath(dir string) (coverFilePath string, err error) {
func coverPath(dir string, counter int) (coverFilePath string, err error) {
if counter > maxRecurse {
return "", recursionError
}

files, err := os.ReadDir(dir)
if err != nil {
return "", err
}

for _, file := range files {
if file.IsDir() {
coverFilePath, err = coverPath(filepath.Join(dir, file.Name()))
coverFilePath, err = coverPath(filepath.Join(dir, file.Name()), counter+1)
if err != nil {
if err == recursionError {
continue
}

return "", err
}

if coverFilePath != "" {
return coverFilePath, err
return coverFilePath, nil
}

continue
Expand Down
45 changes: 45 additions & 0 deletions parse/discover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package parse

import (
"fmt"
"os"
"path/filepath"
"testing"

"gotest.tools/v3/assert"
)

func TestDiscover(t *testing.T) {
testDirSuccess := "./success/t1/t2"
err := os.MkdirAll(testDirSuccess, 0755)
assert.NilError(t, err)

err = os.WriteFile(filepath.Join(testDirSuccess, "cover.out"), []byte("Hello"), 0755)
assert.NilError(t, err)

path, err := Discover()
assert.NilError(t, err)
assert.Equal(t, path, "success/t1/t2/cover.out")

// Clean up success directory
err = os.RemoveAll("./success")
assert.NilError(t, err)

testDirOutOfDepth := "./failure"
for i := 0; i < maxRecurse+1; i++ {
testDirOutOfDepth = filepath.Join(testDirOutOfDepth, fmt.Sprintf("t%d", i))
}

err = os.MkdirAll(testDirOutOfDepth, 0755)
assert.NilError(t, err)

err = os.WriteFile(filepath.Join(testDirOutOfDepth, "cover.out"), []byte("Hello"), 0755)
assert.NilError(t, err)

path, _ = Discover()
assert.Equal(t, path, "")
assert.NilError(t, err)

err = os.RemoveAll("./failure")
assert.NilError(t, err)
}

0 comments on commit db9ab6a

Please sign in to comment.