-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: test fixtures for various unixfs queries
- Loading branch information
Showing
9 changed files
with
671 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package fixtures | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/filecoin-project/lassie/pkg/types" | ||
"github.com/ipfs/go-cid" | ||
"github.com/ipld/go-ipld-prime" | ||
) | ||
|
||
type TestCase struct { | ||
Name string | ||
Root cid.Cid | ||
Path string | ||
Scope types.DagScope | ||
Duplicates bool | ||
ByteRange *types.ByteRange | ||
ExpectedCids []cid.Cid | ||
} | ||
|
||
func (tc TestCase) AsQuery() string { | ||
pp := ipld.ParsePath(tc.Path).String() | ||
if pp != "" { | ||
pp = "/" + pp | ||
} | ||
br := "" | ||
if tc.ByteRange != nil && !tc.ByteRange.IsDefault() { | ||
br = fmt.Sprintf("&entity-bytes=%s", tc.ByteRange.String()) | ||
} | ||
dup := "" | ||
if tc.Duplicates { | ||
dup = "&dups=y" | ||
} | ||
return fmt.Sprintf("/ipfs/%s%s?dag-scope=%s%s%s", tc.Root, pp, tc.Scope, br, dup) | ||
} | ||
|
||
func ParseCase(name, spec, exec string) (TestCase, error) { | ||
lines := strings.Split(exec, "\n") | ||
for len(lines) > 0 && strings.TrimSpace(lines[0]) == "" { | ||
lines = lines[1:] | ||
} | ||
for len(lines) > 0 && strings.TrimSpace(lines[len(lines)-1]) == "" { | ||
lines = lines[:len(lines)-1] | ||
} | ||
specParts := strings.Split(strings.TrimSpace(spec), "?") | ||
if len(specParts) != 2 { | ||
return TestCase{}, errors.New("invalid spec") | ||
} | ||
spec = specParts[0] | ||
query, err := url.ParseQuery(specParts[1]) | ||
if err != nil { | ||
return TestCase{}, err | ||
} | ||
specParts = strings.Split(spec, "/") | ||
if specParts[0] != "" && specParts[1] != "ipfs" { | ||
return TestCase{}, errors.New("invalid spec") | ||
} | ||
root, err := cid.Parse(specParts[2]) | ||
if err != nil { | ||
return TestCase{}, err | ||
} | ||
path := "/" + ipld.ParsePath(strings.Join(specParts[3:], "/")).String() | ||
scope, err := types.ParseDagScope(query.Get("dag-scope")) // required | ||
if err != nil { | ||
return TestCase{}, err | ||
} | ||
duplicates := query.Get("dups") == "y" | ||
var byteRange *types.ByteRange | ||
if query.Get("byte-range") != "" { | ||
if br, err := types.ParseByteRange(query.Get("byte-range")); err != nil { | ||
return TestCase{}, err | ||
} else { | ||
byteRange = &br | ||
} | ||
} | ||
expectedCids := make([]cid.Cid, 0, len(lines)) | ||
for _, line := range lines { | ||
la := strings.Split(line, "|") | ||
c := cid.MustParse(strings.TrimSpace(la[0])) | ||
expectedCids = append(expectedCids, c) | ||
} | ||
return TestCase{ | ||
Name: name, | ||
Root: root, | ||
Path: path, | ||
Scope: scope, | ||
Duplicates: duplicates, | ||
ByteRange: byteRange, | ||
ExpectedCids: expectedCids, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package fixtures | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/ipfs/go-cid" | ||
carstorage "github.com/ipld/go-car/v2/storage" | ||
"github.com/ipld/go-ipld-prime/storage" | ||
"github.com/warpfork/go-testmark" | ||
) | ||
|
||
var Unixfs20mVarietyRoot = cid.MustParse("bafybeifrrglx2issn2had5rtstn3xltla6vxmpjfwfz7o3hapvkynh4zoq") | ||
|
||
const file = "internal/testdata/unixfs_20m_variety." | ||
|
||
func filepath(typ string) (string, error) { | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
return "", err | ||
} | ||
rootInd := strings.LastIndex(wd, "/lassie/pkg/") | ||
if rootInd == -1 { | ||
return "", fmt.Errorf("could not find root of lassie package") | ||
} | ||
filename := wd[:rootInd] + "/lassie/pkg/" + file + typ | ||
fmt.Println("Using", filename) | ||
return filename, nil | ||
} | ||
|
||
func Unixfs20mVarietyReadableStorage() (storage.ReadableStorage, io.Closer, error) { | ||
file, err := filepath("car") | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
carFile, err := os.Open(file) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
reader, err := carstorage.OpenReadable(carFile) | ||
if err != nil { | ||
carFile.Close() | ||
return nil, nil, err | ||
} | ||
return reader, carFile, nil | ||
} | ||
|
||
func Unixfs20mVarietyCases() ([]TestCase, error) { | ||
file, err := filepath("md") | ||
if err != nil { | ||
return nil, err | ||
} | ||
doc, err := testmark.ReadFile(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
doc.BuildDirIndex() | ||
testCases := make([]TestCase, 0) | ||
for _, test := range doc.DirEnt.Children["test"].ChildrenList { | ||
for _, scope := range test.ChildrenList { | ||
tc, err := ParseCase(test.Name+"/"+scope.Name, dstr(scope, "query"), dstr(scope, "execution")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
testCases = append(testCases, tc) | ||
} | ||
} | ||
return testCases, nil | ||
} | ||
|
||
func dstr(dir *testmark.DirEnt, ch string) string { | ||
return string(dir.Children[ch].Hunk.Body) | ||
} |
Oops, something went wrong.