Skip to content

Commit

Permalink
feat(proxy): setup scientist directory for testing (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Dec 4, 2023
1 parent d9f7023 commit 31e8eef
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 79 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
],
"cSpell.words": [
"bodyclose",
"chardata",
"clif",
"cmds",
"cobrass",
Expand Down
Binary file modified dist/darwin/pixa
Binary file not shown.
3 changes: 3 additions & 0 deletions src/app/command/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func (j *Jabber) Scan() language.Tag {
}

func validatePositionalArgs(cmd *cobra.Command, args []string) error {
// TODO: actually, it would be better if we can somehow access the vfs
// instead of using the util.Exist function
//
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions src/app/command/shrink-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
container.MustRegisterParamSet(shrinkPsName, paramSet)
container.MustRegisterParamSet(filesFamName, filesFam)

// TODO: we might need to code this via an anonymous func, store the vfs on
// the bootstrap, then access it from the func, instead of using
// validatePositionalArgs
//
shrinkCommand.Args = validatePositionalArgs

return shrinkCommand
Expand Down
30 changes: 16 additions & 14 deletions src/app/command/shrink-cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
)

type commandTE struct {
message string
args []string
message string
args []string
configPath string
}

type shrinkTE struct {
Expand All @@ -30,27 +31,21 @@ type shrinkTE struct {
func expectValidShrinkCmdInvocation(entry *shrinkTE) {
bootstrap := command.Bootstrap{}

const (
prog = "shrink"
)

// we also prepend the directory name to the command line
//
options := append([]string{prog, entry.directory}, []string{
options := append([]string{helpers.ShrinkCommandName, entry.directory}, []string{
"--dry-run", "--mode", "tidy",
}...)

repo := helpers.Repo(filepath.Join("..", "..", ".."))
configPath := filepath.Join(repo, "test", "data", "configuration")
tester := helpers.CommandTester{
Args: append(options, entry.args...),
Root: bootstrap.Root(func(co *command.ConfigureOptionsInfo) {
co.Detector = &DetectorStub{}
co.Executor = &ExecutorStub{
Name: "magick",
Name: helpers.ProgName,
}
co.Config.Name = helpers.PixaConfigTestFilename
co.Config.ConfigPath = configPath
co.Config.ConfigPath = entry.configPath
}),
}

Expand All @@ -62,16 +57,21 @@ func expectValidShrinkCmdInvocation(entry *shrinkTE) {

var _ = Describe("ShrinkCmd", Ordered, func() {
var (
repo string
l10nPath string
nfs storage.VirtualFS
repo string
l10nPath string
configPath string
nfs storage.VirtualFS
)

BeforeAll(func() {
nfs = storage.UseNativeFS()
repo = helpers.Repo(filepath.Join("..", "..", ".."))

l10nPath = helpers.Path(repo, filepath.Join("test", "data", "l10n"))
Expect(matchers.AsDirectory(l10nPath)).To(matchers.ExistInFS(nfs))

configPath = filepath.Join(repo, "test", "data", "configuration")
Expect(matchers.AsDirectory(configPath)).To(matchers.ExistInFS(nfs))
})

BeforeEach(func() {
Expand Down Expand Up @@ -101,6 +101,7 @@ var _ = Describe("ShrinkCmd", Ordered, func() {
// l10nPath is not set, so we can't set it inside the Entry
//
entry.directory = l10nPath
entry.configPath = configPath
expectValidShrinkCmdInvocation(entry)
},
func(entry *shrinkTE) string {
Expand Down Expand Up @@ -198,6 +199,7 @@ var _ = Describe("ShrinkCmd", Ordered, func() {
args: []string{
"--mirror-path", l10nPath,
},
configPath: configPath,
},
}

Expand Down
38 changes: 23 additions & 15 deletions src/app/proxy/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package proxy_test

import (
"fmt"
"os"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -12,14 +11,11 @@ import (
"github.com/snivilised/cobrass/src/assistant/configuration"
ci18n "github.com/snivilised/cobrass/src/assistant/i18n"
xi18n "github.com/snivilised/extendio/i18n"
"github.com/snivilised/extendio/xfs/storage"
"github.com/snivilised/pixa/src/app/command"
"github.com/snivilised/pixa/src/i18n"
"github.com/snivilised/pixa/src/internal/helpers"
)

const (
relative = "../test/data/configuration"
prog = "magick"
"github.com/snivilised/pixa/src/internal/matchers"
)

func expectValidShrinkCmdInvocation(entry *configTE) {
Expand All @@ -33,15 +29,16 @@ func expectValidShrinkCmdInvocation(entry *configTE) {
}

repo := helpers.Repo(filepath.Join("..", "..", ".."))
configPath := filepath.Join(repo, "test", "data", "configuration")
tester := helpers.CommandTester{
Args: append(options, entry.args...),
Root: bootstrap.Root(func(co *command.ConfigureOptionsInfo) {
co.Detector = &helpers.DetectorStub{}
co.Executor = &helpers.ExecutorStub{
Name: prog,
Name: helpers.ProgName,
}
co.Config.Name = helpers.PixaConfigTestFilename
co.Config.ConfigPath = filepath.Join(repo, "test", "data", "configuration")
co.Config.ConfigPath = configPath
}),
}

Expand All @@ -59,23 +56,34 @@ type configTE struct {
assert func(entry *configTE, actual any)
}

var _ = Describe("Config", func() {
var _ = Describe("Config", Ordered, func() {
var (
config configuration.ViperConfig
l10nPath string
repo string
l10nPath string
configPath string
config configuration.ViperConfig
nfs storage.VirtualFS
)

BeforeAll(func() {
nfs = storage.UseNativeFS()
repo = helpers.Repo(filepath.Join("..", "..", ".."))

l10nPath = helpers.Path(repo, filepath.Join("test", "data", "l10n"))
Expect(matchers.AsDirectory(l10nPath)).To(matchers.ExistInFS(nfs))

configPath = filepath.Join(repo, "test", "data", "configuration")
Expect(matchers.AsDirectory(configPath)).To(matchers.ExistInFS(nfs))
})

BeforeEach(func() {
viper.Reset()
config = &configuration.GlobalViperConfig{}

config.SetConfigType(helpers.PixaConfigType)
config.SetConfigName(helpers.PixaConfigTestFilename)

if _, err := os.Lstat(relative); err != nil {
Fail("🔥 can't find config path")
}
config.AddConfigPath(relative)
config.AddConfigPath(configPath)
if err := config.ReadInConfig(); err != nil {
Fail(fmt.Sprintf("🔥 can't read config (err: '%v')", err))
}
Expand Down
9 changes: 6 additions & 3 deletions src/app/proxy/enter-shrink.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func (e *ShrinkEntry) PrincipalOptionsFn(o *nav.TraverseOptions) {
}

runner := e.Registry.Get()
e.Registry.Put(runner)
defer e.Registry.Put(runner)

return e.Program.Execute(clif.Expand(positional, e.ThirdPartyCL)...)
return runner.OnNewItem(item, clif.Expand(positional, e.ThirdPartyCL)...)
},
}
}
Expand Down Expand Up @@ -136,7 +136,10 @@ func (e *ShrinkEntry) resumeFn(item *nav.TraverseItem) error {
fmt.Sprintf("'%v'", item.Path),
}

return e.Program.Execute(clif.Expand(positional, e.ThirdPartyCL)...)
runner := e.Registry.Get()
defer e.Registry.Put(runner)

return runner.OnNewItem(item, clif.Expand(positional, e.ThirdPartyCL)...)
}

func (e *ShrinkEntry) run(config configuration.ViperConfig) error {
Expand Down
11 changes: 8 additions & 3 deletions src/app/proxy/entry-base.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func summariseAfter(result *nav.TraverseResult, err error) {
folders := result.Metrics.Count(nav.MetricNoFoldersInvokedEn)
summary := fmt.Sprintf("files: %v, folders: %v", files, folders)
message := lo.Ternary(err == nil,
fmt.Sprintf("navigation completed (%v) ✔️ [%v]", summary, measure),
fmt.Sprintf("error occurred during navigation (%v) [%v]", err, measure),
fmt.Sprintf("navigation completed ok (%v) 💝 [%v]", summary, measure),
fmt.Sprintf("error occurred during navigation (%v)❤️💔 [%v]", err, measure),
)
fmt.Println(message)
}
Expand Down Expand Up @@ -89,12 +89,17 @@ func (e *EntryBase) ConfigureOptions(o *nav.TraverseOptions) {
// TODO: get the runner type properly, instead of hard coding to Sampler
//
e.Registry = NewRunnerRegistry(&NewRunnerParams{
Type: RunnerTypeSamplerEn, // !!!
Type: RunnerTypeSamplerEn, // TODO: to come from an arg !!!
Options: e.Options,
Program: e.Program,
})
}

func ResolvePath(path string) string {
if path == "" {
return path
}

result := path

if result[0] == '~' {
Expand Down
12 changes: 9 additions & 3 deletions src/app/proxy/item-runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type itemRunner struct {
sharedInfo *SharedRunnerInfo
shared *SharedRunnerInfo
}

func (r *itemRunner) Reset() {
Expand All @@ -15,14 +15,20 @@ type SamplerRunner struct {
itemRunner
}

func (r *SamplerRunner) OnNewItem(item *nav.TraverseItem) {
func (r *SamplerRunner) OnNewItem(item *nav.TraverseItem, args ...string) error {
_ = item
_ = args

return r.shared.program.Execute(args...)
}

type FullRunner struct {
itemRunner
}

func (r *FullRunner) OnNewItem(item *nav.TraverseItem) {
func (r *FullRunner) OnNewItem(item *nav.TraverseItem, args ...string) error {
_ = item
_ = args

return nil
}
3 changes: 2 additions & 1 deletion src/app/proxy/proxy-defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ const (
type SharedRunnerInfo struct {
Type RunnerTypeEnum
Options *nav.TraverseOptions
program Executor
}

// ItemController
type ItemRunner interface {
OnNewItem(item *nav.TraverseItem)
OnNewItem(item *nav.TraverseItem, args ...string) error
Reset()
}
6 changes: 4 additions & 2 deletions src/app/proxy/runner-registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type NewRunnerParams struct {
Type RunnerTypeEnum
Options *nav.TraverseOptions
Program Executor
}

func NewRunnerRegistry(params *NewRunnerParams) *RunnerRegistry {
Expand All @@ -21,17 +22,18 @@ func NewRunnerRegistry(params *NewRunnerParams) *RunnerRegistry {
case RunnerTypeFullEn:
return &FullRunner{
itemRunner: itemRunner{
sharedInfo: &SharedRunnerInfo{
shared: &SharedRunnerInfo{
Type: params.Type,
Options: params.Options,
program: params.Program,
},
},
}

case RunnerTypeSamplerEn:
return &SamplerRunner{
itemRunner: itemRunner{
sharedInfo: &SharedRunnerInfo{
shared: &SharedRunnerInfo{
Type: params.Type,
Options: params.Options,
},
Expand Down
Loading

0 comments on commit 31e8eef

Please sign in to comment.