Skip to content

Commit

Permalink
fix(readFile): Remove workdir path from file path to generate valid f…
Browse files Browse the repository at this point in the history
…ilename in tests results (#174)
  • Loading branch information
richardlt authored and fsamin committed Nov 14, 2018
1 parent ed86c4e commit 68cde7c
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions executors/readfile/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"time"

Expand Down Expand Up @@ -70,7 +69,7 @@ func (Executor) Run(testCaseContext venom.TestCaseContext, l venom.Logger, step

start := time.Now()

result, errr := e.readfile(path.Join(workdir, e.Path))
result, errr := e.readfile(workdir)
if errr != nil {
result.Err = errr.Error()
}
Expand All @@ -82,21 +81,23 @@ func (Executor) Run(testCaseContext venom.TestCaseContext, l venom.Logger, step
return executors.Dump(result)
}

func (e *Executor) readfile(path string) (Result, error) {
func (e *Executor) readfile(workdir string) (Result, error) {
result := Result{Executor: *e}

fileInfo, _ := os.Stat(path)
absPath := filepath.Join(workdir, e.Path)

fileInfo, _ := os.Stat(absPath)
if fileInfo != nil && fileInfo.IsDir() {
path = filepath.Dir(path)
absPath = filepath.Dir(absPath)
}

filesPath, errg := zglob.Glob(path)
filesPath, errg := zglob.Glob(absPath)
if errg != nil {
return result, fmt.Errorf("Error reading files on path:%s :%s", path, errg)
return result, fmt.Errorf("Error reading files on path:%s :%s", absPath, errg)
}

if len(filesPath) == 0 {
return result, fmt.Errorf("Invalid path '%s' or file not found", path)
return result, fmt.Errorf("Invalid path '%s' or file not found", absPath)
}

var content string
Expand All @@ -112,6 +113,11 @@ func (e *Executor) readfile(path string) (Result, error) {
}
defer f.Close()

relativeName, err := filepath.Rel(workdir, f.Name())
if err != nil {
return result, fmt.Errorf("Error cannot evaluate relative path to file at %s: %s", f.Name(), err)
}

b, errr := ioutil.ReadAll(f)
if errr != nil {
return result, fmt.Errorf("Error while reading file: %s", errr)
Expand All @@ -123,17 +129,16 @@ func (e *Executor) readfile(path string) (Result, error) {
return result, fmt.Errorf("Error while compute md5sum: %s", err)
}

md5sum[f.Name()] = hex.EncodeToString(h.Sum(nil))
md5sum[relativeName] = hex.EncodeToString(h.Sum(nil))

stat, errs := f.Stat()
if errs != nil {
return result, fmt.Errorf("Error while compute file size: %s", errs)
}

size[f.Name()] = stat.Size()
modtime[f.Name()] = stat.ModTime().Unix()
mod[f.Name()] = stat.Mode().String()

size[relativeName] = stat.Size()
modtime[relativeName] = stat.ModTime().Unix()
mod[relativeName] = stat.Mode().String()
}

result.Content = content
Expand Down

0 comments on commit 68cde7c

Please sign in to comment.