-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util/testutil/addcat/addcat: Factor out add/cat/verify helper
Save some repetitive logic in the integration tests. While we're at it, replace coreunix.Add with a directo call to BuildDagFromReader. coreunix.Add is basically a wrapper around BuildDagFromReader that returns a string-ified key instead of a DAG node. That's what we want here, so it's a few more lines to inline the key extraction (mostly due to Go's explicit error handling [1], which makes method chaining not idomatic [2]). But I think a bit of local error-checking is a reasonable price for a more concise API, where there's only one function for adding file nodes from a reader. I'd planned to add the AddCat helper to the testutil package, but that triggered some cyclic imports: $ make test cd cmd/ipfs && go build -i import cycle not allowed package github.com/ipfs/go-ipfs/cmd/ipfs imports github.com/ipfs/go-ipfs/commands imports github.com/ipfs/go-ipfs/core imports github.com/ipfs/go-ipfs/blockservice imports github.com/ipfs/go-ipfs/exchange/bitswap imports github.com/ipfs/go-ipfs/exchange/bitswap/testnet imports github.com/ipfs/go-ipfs/p2p/net/mock imports github.com/ipfs/go-ipfs/p2p/test/util imports github.com/ipfs/go-ipfs/util/testutil imports github.com/ipfs/go-ipfs/core/coreunix imports github.com/ipfs/go-ipfs/importer imports github.com/ipfs/go-ipfs/importer/balanced imports github.com/ipfs/go-ipfs/importer/helpers imports github.com/ipfs/go-ipfs/merkledag imports github.com/ipfs/go-ipfs/blockservice import cycle not allowed package github.com/ipfs/go-ipfs/cmd/ipfs imports github.com/ipfs/go-ipfs/commands imports github.com/ipfs/go-ipfs/core imports github.com/ipfs/go-ipfs/blockservice imports github.com/ipfs/go-ipfs/exchange/bitswap imports github.com/ipfs/go-ipfs/exchange/bitswap/testnet imports github.com/ipfs/go-ipfs/p2p/net/mock imports github.com/ipfs/go-ipfs/p2p/test/util imports github.com/ipfs/go-ipfs/util/testutil imports github.com/ipfs/go-ipfs/core Makefile:27: recipe for target 'build' failed make: *** [build] Error 1 I think the proper workaround is to split tests that import test-only libraries out into their own packages. For example, exchange/bitswap/bitswap_test.go is in the bitswap package, but imports the exchange/bitswap/testnet package, which in turn pulls in mock, testutil, etc. The easiest workaround seems to be using external tests [3] (e.g. bitswap_test for exchange/bitswap/bitswap_test.go). I tried to apply this approach, but the current codebase had too many crosslinks (e.g. internal tests that import test-only packages but also use internal utilities from their current package), so I gave up. I think gradually converting test files into external tests is a good thing, but it's too much to bite off for this feature branch. [1]: https://golang.org/doc/faq#exceptions [2]: http://stackoverflow.com/a/27300387 [3]: http://dave.cheney.net/2014/12/01/five-suggestions-for-setting-up-a-go-project
- Loading branch information
Showing
6 changed files
with
67 additions
and
81 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
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,44 @@ | ||
package testutil | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
|
||
core "github.com/ipfs/go-ipfs/core" | ||
coreunix "github.com/ipfs/go-ipfs/core/coreunix" | ||
importer "github.com/ipfs/go-ipfs/importer" | ||
chunk "github.com/ipfs/go-ipfs/importer/chunk" | ||
errors "github.com/ipfs/go-ipfs/util/debugerror" | ||
) | ||
|
||
func AddCat(adder *core.IpfsNode, catter *core.IpfsNode, data string) error { | ||
dagNode, err := importer.BuildDagFromReader( | ||
strings.NewReader(data), | ||
adder.DAG, | ||
adder.Pinning.GetManual(), | ||
chunk.DefaultSplitter) | ||
if err != nil { | ||
return err | ||
} | ||
key, err := dagNode.Key() | ||
if err != nil { | ||
return err | ||
} | ||
added := key.String() | ||
|
||
reader, err := coreunix.Cat(catter, added) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// verify | ||
var buf bytes.Buffer | ||
_, err = buf.ReadFrom(reader) | ||
if err != nil { | ||
return err | ||
} | ||
if buf.String() != data { | ||
return errors.New("catted data does not match added data") | ||
} | ||
return nil | ||
} |