Skip to content

Commit

Permalink
test(logic): allow mock fs
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Mar 16, 2023
1 parent 3df9cb2 commit e09228e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ mock: ## Generate all the mocks (for tests)
@echo "${COLOR_CYAN} 🧱 Generating all the mocks${COLOR_RESET}"
@go install github.com/golang/mock/[email protected]
@mockgen -source=x/logic/types/expected_keepers.go -package testutil -destination x/logic/testutil/expected_keepers_mocks.go
@mockgen -source=x/logic/fs/fs.go -package testutil -destination x/logic/testutil/fs_mocks.go

## Release:
.PHONY: release-assets
Expand Down
3 changes: 1 addition & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -1017,7 +1016,7 @@ func (app *App) SimulationManager() *module.SimulationManager {

// provideFS is used to provide the virtual file system used for the logic module
// to load external file.
func (app *App) provideFS(ctx context.Context) fs.FS {
func (app *App) provideFS(ctx context.Context) logicfs.FS {
wasmHandler := logicfs.NewWasmHandler(app.WasmKeeper)
return logicfs.NewVirtualFS(ctx, []logicfs.URIHandler{wasmHandler})
}
6 changes: 5 additions & 1 deletion x/logic/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import (
"io/fs"
)

type FS interface {
fs.FS
}

// VirtualFS is the custom virtual file system used into the blockchain.
// It will hold a list of handler that can resolve file URI and return the corresponding binary file.
type VirtualFS struct {
ctx goctx.Context
router Router
}

var _ fs.FS = (*VirtualFS)(nil)
var _ FS = (*VirtualFS)(nil)

// NewVirtualFS return a new VirtualFS object that will handle all virtual file on the interpreter.
// File can be provided from different sources like CosmWasm cw-storage smart contract.
Expand Down
2 changes: 1 addition & 1 deletion x/logic/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package keeper
import (
goctx "context"
"fmt"
"io/fs"

"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/okp4/okp4d/x/logic/fs"
"github.com/okp4/okp4d/x/logic/types"
"github.com/tendermint/tendermint/libs/log"
)
Expand Down
50 changes: 50 additions & 0 deletions x/logic/testutil/fs_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions x/logic/testutil/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ichiban/prolog"
"github.com/ichiban/prolog/engine"
"github.com/okp4/okp4d/x/logic/fs"
)

// NewInterpreterMust returns a new Interpreter with the given context or panics if it fails.
Expand All @@ -16,6 +17,7 @@ func NewInterpreterMust(ctx context.Context) (interpreter *prolog.Interpreter) {
interpreter.Register3(engine.NewAtom("op"), engine.Op)
interpreter.Register3(engine.NewAtom("compare"), engine.Compare)
interpreter.Register2(engine.NewAtom("="), engine.Unify)
interpreter.Register1(engine.NewAtom("consult"), engine.Consult)

err := interpreter.Compile(ctx, `
:-(op(1200, xfx, ':-')).
Expand All @@ -34,6 +36,13 @@ func NewInterpreterMust(ctx context.Context) (interpreter *prolog.Interpreter) {
return
}

func NewInterpreterWithFSMust(ctx context.Context, filesystem fs.FS) (interpreter *prolog.Interpreter) {
interpreter = NewInterpreterMust(ctx)
interpreter.FS = filesystem

return
}

// CompileMust compiles the given source code and panics if it fails.
// This is a convenience function for testing.
func CompileMust(ctx context.Context, interpreter *prolog.Interpreter, s string, args ...interface{}) {
Expand Down

0 comments on commit e09228e

Please sign in to comment.