Skip to content

Commit

Permalink
fix(fsi): Add relative directory to fsi body path. Parse body for sta…
Browse files Browse the repository at this point in the history
…tus.
  • Loading branch information
dustmop committed Sep 6, 2019
1 parent c6c3a36 commit 4284b5e
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 12 deletions.
4 changes: 2 additions & 2 deletions api/fsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestNoHistory(t *testing.T) {
}
// Handle temporary directory by replacing the temp part with a shorter string.
resultBody = strings.Replace(actualBody, initDir, initSubdir, -1)
templateBody := `{"data":[{"sourceFile":"fsi_init_dir/meta.json","component":"meta","type":"add","message":"","mtime":"%s"},{"sourceFile":"fsi_init_dir/schema.json","component":"schema","type":"add","message":"","mtime":"%s"},{"sourceFile":"body.csv","component":"body","type":"add","message":"","mtime":"%s"}],"meta":{"code":200}}`
templateBody := `{"data":[{"sourceFile":"fsi_init_dir/meta.json","component":"meta","type":"add","message":"","mtime":"%s"},{"sourceFile":"fsi_init_dir/schema.json","component":"schema","type":"add","message":"","mtime":"%s"},{"sourceFile":"fsi_init_dir/body.csv","component":"body","type":"add","message":"","mtime":"%s"}],"meta":{"code":200}}`
expectBody = fmt.Sprintf(templateBody, metaMtime, schemaMtime, bodyMtime)
if diff := cmp.Diff(expectBody, resultBody); diff != "" {
t.Errorf("api response (-want +got):\n%s", diff)
Expand Down Expand Up @@ -294,7 +294,7 @@ func TestCheckoutAndRestore(t *testing.T) {
}
// Handle temporary directory by replacing the temp part with a shorter string.
resultBody := strings.Replace(actualBody, workDir, "", -1)
templateBody := `{"data":[{"sourceFile":"/meta.json","component":"meta","type":"modified","message":"","mtime":"%s"},{"sourceFile":"/dataset.json","component":"structure","type":"unmodified","message":"","mtime":"%s"},{"sourceFile":"/schema.json","component":"schema","type":"unmodified","message":"","mtime":"%s"},{"sourceFile":"body.csv","component":"body","type":"unmodified","message":"","mtime":"%s"}],"meta":{"code":200}}`
templateBody := `{"data":[{"sourceFile":"/meta.json","component":"meta","type":"modified","message":"","mtime":"%s"},{"sourceFile":"/dataset.json","component":"structure","type":"unmodified","message":"","mtime":"%s"},{"sourceFile":"/schema.json","component":"schema","type":"unmodified","message":"","mtime":"%s"},{"sourceFile":"/body.csv","component":"body","type":"unmodified","message":"","mtime":"%s"}],"meta":{"code":200}}`
expectBody = fmt.Sprintf(templateBody, metaMtime, datasetMtime, schemaMtime, bodyMtime)
if diff := cmp.Diff(expectBody, resultBody); diff != "" {
t.Errorf("api response (-want +got):\n%s", diff)
Expand Down
43 changes: 43 additions & 0 deletions cmd/fsi_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,49 @@ fix these problems before saving this dataset
}
}

// Test that status displays parse errors even for the body component
func TestBodyParseError(t *testing.T) {
fr := NewFSITestRunner(t, "qri_test_status_parse_error")
defer fr.Delete()

// Save a dataset containing a body.json and meta component
err := fr.ExecCommand("qri save --body=testdata/movies/body_two.json --file=testdata/movies/meta_override.yaml me/bad_body")
if err != nil {
t.Fatalf(err.Error())
}

// Change to a temporary directory.
fr.ChdirToRoot()

// Checkout the newly created dataset.
if err = fr.ExecCommand("qri checkout me/bad_body"); err != nil {
t.Fatalf(err.Error())
}

_ = fr.ChdirToWorkDir("bad_body")

// Modify the meta.json so that it fails to parse.
if err = ioutil.WriteFile("body.json", []byte(`{"title": "hello}`), os.ModePerm); err != nil {
t.Fatalf(err.Error())
}

// Status, check that status shows the parse error.
if err = fr.ExecCommand("qri status"); err != nil {
t.Fatalf(err.Error())
}

output := fr.GetCommandOutput()
expect := `for linked dataset [test_peer/bad_body]
parse error: body (source: body.json)
fix these problems before saving this dataset
`
if diff := cmpTextLines(expect, output); diff != "" {
t.Errorf("qri status (-want +got):\n%s", diff)
}
}

// Test that parse errors are also properly shown for schema.
func TestStatusParseErrorForSchema(t *testing.T) {
fr := NewFSITestRunner(t, "qri_test_status_parse_error_for_schema")
Expand Down
2 changes: 1 addition & 1 deletion fsi/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func GetBody(dirPath string, format dataset.DataFormat, fcfg dataset.FormatConfi
return nil, fmt.Errorf("no body found")
}

f, err := os.Open(filepath.Join(dirPath, bodyFileStat.Path))
f, err := os.Open(bodyFileStat.Path)
if err != nil {
return nil, err
}
Expand Down
53 changes: 48 additions & 5 deletions fsi/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/ghodss/yaml"
"github.com/qri-io/dataset"
"github.com/qri-io/dataset/detect"
"github.com/qri-io/dataset/dsio"
)

const (
Expand Down Expand Up @@ -90,14 +92,39 @@ func ReadDir(dir string) (ds *dataset.Dataset, fileMap, problems map[string]File
bodyFormat = "json"
}

bodyFilename := ""
bodyFilepath := ""
if bodyFormat != "" {
bodyFilename = fmt.Sprintf("body.%s", bodyFormat)
if err = addFile(componentNameBody, bodyFilename, mtime); err != nil {
bodyFilepath = filepath.Join(dir, fmt.Sprintf("body.%s", bodyFormat))
if err != nil {
return ds, fileMap, problems, err
}
if ds.BodyPath == "" {
ds.BodyPath = filepath.Join(dir, bodyFilename)
bodyOkay := false
file, err := os.Open(bodyFilepath)
if err == nil {
// Read each entry of the body.
entries, err := OpenEntryReader(file, bodyFormat)
if err == nil {
err = dsio.EachEntry(entries, func(int, dsio.Entry, error) error { return nil })
if err == nil {
bodyOkay = true
}
}
}
err = nil
if bodyOkay {
// If body parsed okay, add the body component.
if err = addFile(componentNameBody, bodyFilepath, mtime); err != nil {
return ds, fileMap, problems, err
}
if ds.BodyPath == "" {
ds.BodyPath = bodyFilepath
}
} else {
// Else, record a problem.
if problems == nil {
problems = make(map[string]FileStat)
}
problems[componentNameBody] = FileStat{Path: bodyFilepath, Mtime: mtime}
}
}

Expand Down Expand Up @@ -356,3 +383,19 @@ func DeleteComponents(removeList []string, fileMap map[string]FileStat, dirPath
}
return nil
}

// OpenEntryReader opens a entry reader for the file, determining the schema automatically
func OpenEntryReader(file *os.File, format string) (dsio.EntryReader, error) {
st := dataset.Structure{Format: format}
schema, _, err := detect.Schema(&st, file)
if err != nil {
return nil, err
}
file.Seek(0, 0)
st.Schema = schema
entries, err := dsio.NewEntryReader(&st, file)
if err != nil {
return nil, err
}
return entries, nil
}
3 changes: 1 addition & 2 deletions fsi/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"time"

Expand Down Expand Up @@ -134,7 +133,7 @@ func (fsi *FSI) Status(dir string) (changes []StatusItem, err error) {

// Set body file from local filesystem.
if bodyStat, ok := fileMap[componentNameBody]; ok {
bf, err := os.Open(filepath.Join(dir, bodyStat.Path))
bf, err := os.Open(bodyStat.Path)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions fsi/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func TestStatusValid(t *testing.T) {
actual += strings.Replace(fmt.Sprintf("%s", ch), paths.firstDir, ".", 1)
}
// Construct the expected repsonse by getting the real timestamp from each component.
expectList := []string{"commit", "meta", "schema", "structure", "transform", "viz", "body"}
expectList := []string{"body", "commit", "meta", "schema", "structure", "transform", "viz"}
expect := ""
for _, cmpName := range expectList {
var componentFile string
if cmpName == "body" {
componentFile = "body.csv"
componentFile = "./body.csv"
} else {
componentFile = fmt.Sprintf("./%s.json", cmpName)
}
Expand Down

0 comments on commit 4284b5e

Please sign in to comment.