-
Couldn't load subscription status.
- Fork 5
feat: add test init #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,239 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "math/big" | ||
| "os" | ||
|
|
||
| "github.com/formancehq/numscript/internal/analysis" | ||
| "github.com/formancehq/numscript/internal/interpreter" | ||
| "github.com/formancehq/numscript/internal/parser" | ||
| "github.com/formancehq/numscript/internal/specs_format" | ||
| "github.com/formancehq/numscript/internal/utils" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type testInitArgs struct { | ||
| path string | ||
| } | ||
|
|
||
| func getTestInitCmd() *cobra.Command { | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "test-init path", | ||
| Short: "Create a specs file for the given numscript", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| err := runTestInitCmd(testInitArgs{ | ||
| path: args[0], | ||
| }) | ||
|
|
||
| if err != nil { | ||
| cmd.SilenceErrors = true | ||
| cmd.SilenceUsage = true | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
|
|
||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func mkDefaultVar(decl parser.VarDeclaration, checkResult analysis.CheckResult) string { | ||
| defaultAmt := 100 | ||
| defaultCurr := "USD/2" | ||
|
|
||
| asset := checkResult.GetVarDeclType(decl) | ||
| switch asset := asset.(type) { | ||
| case *analysis.TAsset: | ||
| defaultCurr = string(*asset) | ||
| } | ||
|
|
||
| switch decl.Type.Name { | ||
|
|
||
| case analysis.TypeMonetary: | ||
| return fmt.Sprintf("%s %d", defaultCurr, defaultAmt) | ||
|
|
||
| case analysis.TypeNumber: | ||
| return fmt.Sprintf("%d", defaultAmt) | ||
|
|
||
| case analysis.TypeAccount: | ||
| return decl.Name.Name | ||
|
|
||
| case analysis.TypeString: | ||
| return decl.Name.Name | ||
|
|
||
| case analysis.TypePortion: | ||
| return "1/2" | ||
|
|
||
| case analysis.TypeAsset: | ||
| return string(defaultCurr) | ||
| } | ||
|
|
||
| return "" | ||
| } | ||
|
|
||
| func MakeSpecsFile(source string) (specs_format.Specs, error) { | ||
| parseResult := parser.Parse(source) | ||
| if len(parseResult.Errors) != 0 { | ||
| fmt.Fprint(os.Stderr, parser.ParseErrorsToString(parseResult.Errors, source)) | ||
| return specs_format.Specs{}, fmt.Errorf("parsing failed") | ||
| } | ||
|
|
||
| checkResult := analysis.CheckProgram(parseResult.Value) | ||
|
|
||
| vars := map[string]string{} | ||
| if parseResult.Value.Vars != nil { | ||
| for _, decl := range parseResult.Value.Vars.Declarations { | ||
| if decl.Origin != nil { | ||
| continue | ||
| } | ||
|
|
||
| value := mkDefaultVar(decl, checkResult) | ||
| vars[decl.Name.Name] = value | ||
| } | ||
| } | ||
|
|
||
| return makeSpecsFile( | ||
| parseResult.Value, | ||
| vars, | ||
| map[string]struct{}{}, | ||
| big.NewInt(100), | ||
| ) | ||
| } | ||
|
|
||
| func makeSpecsFile( | ||
| program parser.Program, | ||
| vars map[string]string, | ||
| featureFlags map[string]struct{}, | ||
| defaultBalance *big.Int, | ||
| ) (specs_format.Specs, error) { | ||
|
|
||
| store := TestInitStore{ | ||
| DefaultBalance: defaultBalance, | ||
| Balances: make(interpreter.Balances), | ||
| Meta: make(interpreter.AccountsMetadata), | ||
| } | ||
|
|
||
| res, iErr := interpreter.RunProgram( | ||
| context.Background(), | ||
| program, | ||
| vars, | ||
| store, | ||
| featureFlags, | ||
| ) | ||
|
|
||
| if iErr != nil { | ||
| missingFundsErr, missingFunds := iErr.(interpreter.MissingFundsErr) | ||
| if missingFunds { | ||
| // TODO we could have better heuristics with a balance for each account/asset pair | ||
| return makeSpecsFile( | ||
| program, | ||
| vars, | ||
| featureFlags, | ||
| &missingFundsErr.Needed, | ||
| ) | ||
| } | ||
|
|
||
| expFeatErr, missingFeatureFlag := iErr.(interpreter.ExperimentalFeature) | ||
| if missingFeatureFlag { | ||
| featureFlags[expFeatErr.FlagName] = struct{}{} | ||
| return makeSpecsFile( | ||
| program, | ||
| vars, | ||
| featureFlags, | ||
| &missingFundsErr.Needed, | ||
| ) | ||
| } | ||
|
|
||
| return specs_format.Specs{}, iErr | ||
| } | ||
|
|
||
| var featureFlags_ []string | ||
| for k := range featureFlags { | ||
| featureFlags_ = append(featureFlags_, k) | ||
| } | ||
|
|
||
| specs := specs_format.Specs{ | ||
| Schema: "https://raw.githubusercontent.com/formancehq/numscript/main/specs.schema.json", | ||
| Balances: store.Balances, | ||
| Vars: vars, | ||
| FeatureFlags: featureFlags_, | ||
| TestCases: []specs_format.TestCase{ | ||
| { | ||
| It: "example spec", | ||
| ExpectPostings: res.Postings, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| return specs, nil | ||
| } | ||
|
|
||
| func runTestInitCmd(opts testInitArgs) error { | ||
| // TODO check there isn't a specsfile already | ||
|
|
||
| numscriptContent, err := os.ReadFile(opts.path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| specs, err := MakeSpecsFile(string(numscriptContent)) | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| marshaled, _ := json.MarshalIndent(specs, "", " ") | ||
|
|
||
| os.WriteFile(opts.path+".specs.json", marshaled, 0644) | ||
|
|
||
| fmt.Printf("✅ Created specs file: %s.specs.json\n", opts.path) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| type TestInitStore struct { | ||
| DefaultBalance *big.Int | ||
| Balances interpreter.Balances | ||
| Meta interpreter.AccountsMetadata | ||
| } | ||
|
|
||
| func (s TestInitStore) GetBalances(_ context.Context, q interpreter.BalanceQuery) (interpreter.Balances, error) { | ||
| outputBalance := interpreter.Balances{} | ||
| for queriedAccount, queriedCurrencies := range q { | ||
|
|
||
| for _, curr := range queriedCurrencies { | ||
| amt := utils.NestedMapGetOrPutDefault(s.Balances, queriedAccount, curr, func() *big.Int { | ||
| return new(big.Int).Set(s.DefaultBalance) | ||
| }) | ||
|
|
||
| outputAccountBalance := utils.MapGetOrPutDefault(outputBalance, queriedAccount, func() interpreter.AccountBalance { | ||
| return interpreter.AccountBalance{} | ||
| }) | ||
|
|
||
| outputAccountBalance[curr] = new(big.Int).Set(amt) | ||
| } | ||
| } | ||
|
|
||
| return outputBalance, nil | ||
| } | ||
|
|
||
| func (s TestInitStore) GetAccountsMetadata(c context.Context, q interpreter.MetadataQuery) (interpreter.AccountsMetadata, error) { | ||
| outputMeta := interpreter.AccountsMetadata{} | ||
| for queriedAccount, queriedCurrencies := range q { | ||
| for _, curr := range queriedCurrencies { | ||
| outputAccountMeta := utils.MapGetOrPutDefault(outputMeta, queriedAccount, func() interpreter.AccountMetadata { | ||
| return interpreter.AccountMetadata{} | ||
| }) | ||
| outputAccountMeta[curr] = "" | ||
| } | ||
| } | ||
|
|
||
| return outputMeta, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix default balance reuse for feature-flag retries.
...
...
return makeSpecsFile(
program,
vars,
featureFlags,
&missingFundsErr.Needed,
)
return makeSpecsFile(
program,
vars,
featureFlags,
defaultBalance,
)
if err := os.WriteFile(...); err != nil { return err }
if err := os.WriteFile(...); err != nil {
return err
}
fmt.Printf...
internal/cmd/test_init.go around lines 145-152 and 193-197: Fix two issues—(1)
when retrying with feature-flag errors we accidentally pass
&missingFundsErr.Needed (reusing the pointer) into makeSpecsFile; replace that
argument with the defaultBalance value so each retry uses the fresh default
balance instead of the previous error's pointer. (2) os.WriteFile errors are
ignored around lines 193-197; change the code to check the error returned by
os.WriteFile and return/handle it (and only print the success message after a
successful write) so file-write failures propagate and don’t leave the caller
unaware.