From 5d9c3858dffebd2bc9995315b16369216818afb1 Mon Sep 17 00:00:00 2001 From: Rustam Gilyazov <16064414+rusq@users.noreply.github.com> Date: Fri, 26 May 2023 19:31:09 +1000 Subject: [PATCH] tests and doc --- cmd/slackdump/internal/apiconfig/apiconfig.go | 9 +- .../internal/apiconfig/apiconfig_test.go | 85 ++++++++++++++++--- cmd/slackdump/internal/apiconfig/new.go | 3 +- cmd/slackdump/internal/apiconfig/schema.json | 8 +- slackdump.1 | 7 +- 5 files changed, 89 insertions(+), 23 deletions(-) diff --git a/cmd/slackdump/internal/apiconfig/apiconfig.go b/cmd/slackdump/internal/apiconfig/apiconfig.go index 7108686d..abf1ef7e 100644 --- a/cmd/slackdump/internal/apiconfig/apiconfig.go +++ b/cmd/slackdump/internal/apiconfig/apiconfig.go @@ -14,6 +14,9 @@ import ( "github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base" ) +// schemaJSONpath is the path to the schema JSON file for the limits yaml +// configuration file. +// TODO: update once released const schemaJSONpath = "https://raw.githubusercontent.com/rusq/slackdump/cli-remake/cmd/slackdump/internal/apiconfig/schema.json" var CmdConfig = &base.Command{ @@ -41,7 +44,7 @@ func Load(filename string) (slackdump.Limits, error) { } defer f.Close() - return readLimits(f) + return applyLimits(f) } // Save saves the config to the file. @@ -55,7 +58,9 @@ func Save(filename string, limits slackdump.Limits) error { return writeLimits(f, limits) } -func readLimits(r io.Reader) (slackdump.Limits, error) { +// applyLimits reads the limits from the reader, validates them and applies to +// the global config. +func applyLimits(r io.Reader) (slackdump.Limits, error) { var limits slackdump.Limits dec := yaml.NewDecoder(r) dec.KnownFields(true) diff --git a/cmd/slackdump/internal/apiconfig/apiconfig_test.go b/cmd/slackdump/internal/apiconfig/apiconfig_test.go index b8445946..95e2d23b 100644 --- a/cmd/slackdump/internal/apiconfig/apiconfig_test.go +++ b/cmd/slackdump/internal/apiconfig/apiconfig_test.go @@ -1,33 +1,36 @@ package apiconfig import ( + "bytes" "io" "reflect" "strings" "testing" "github.com/rusq/slackdump/v2" + "github.com/stretchr/testify/assert" ) const ( - sampleLimitsYaml = `workers: 4 + sampleLimitsYaml = `# yaml-language-server: $schema=https://raw.githubusercontent.com/rusq/slackdump/cli-remake/cmd/slackdump/internal/apiconfig/schema.json +workers: 4 download_retries: 3 tier_2: - boost: 20 - burst: 1 - retries: 20 + boost: 20 + burst: 1 + retries: 20 tier_3: - boost: 120 - burst: 1 - retries: 3 + boost: 120 + burst: 1 + retries: 3 tier_4: - boost: 10 - burst: 1 - retries: 3 + boost: 10 + burst: 1 + retries: 3 per_request: - conversations: 100 - channels: 100 - replies: 200 + conversations: 100 + channels: 100 + replies: 200 ` // workers set to 55 in this one, tier2.retries to 330 updatedConfigYaml = `workers: 55 @@ -51,6 +54,31 @@ per_request: ` ) +var testLimits = slackdump.Limits{ + Workers: 4, + DownloadRetries: 3, + Tier2: slackdump.TierLimit{ + Boost: 20, + Burst: 1, + Retries: 20, + }, + Tier3: slackdump.TierLimit{ + Boost: 120, + Burst: 1, + Retries: 3, + }, + Tier4: slackdump.TierLimit{ + Boost: 10, + Burst: 1, + Retries: 3, + }, + Request: slackdump.RequestLimit{ + Conversations: 100, + Channels: 100, + Replies: 200, + }, +} + func Test_readConfig(t *testing.T) { type args struct { r io.Reader @@ -105,7 +133,7 @@ func Test_readConfig(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := readLimits(tt.args.r) + got, err := applyLimits(tt.args.r) t.Log(tt.name) if (err != nil) != tt.wantErr { t.Errorf("readConfig() error = %v, wantErr %v", err, tt.wantErr) @@ -117,3 +145,32 @@ func Test_readConfig(t *testing.T) { }) } } + +func Test_writeLimits(t *testing.T) { + type args struct { + cfg slackdump.Limits + } + tests := []struct { + name string + args args + wantW string + wantErr bool + }{ + { + "writes limits and comments", + args{testLimits}, + sampleLimitsYaml, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := &bytes.Buffer{} + if err := writeLimits(w, tt.args.cfg); (err != nil) != tt.wantErr { + t.Errorf("writeLimits() error = %v, wantErr %v", err, tt.wantErr) + return + } + assert.Equal(t, tt.wantW, w.String()) + }) + } +} diff --git a/cmd/slackdump/internal/apiconfig/new.go b/cmd/slackdump/internal/apiconfig/new.go index 116c12cc..f3cc0f5e 100644 --- a/cmd/slackdump/internal/apiconfig/new.go +++ b/cmd/slackdump/internal/apiconfig/new.go @@ -65,6 +65,7 @@ func runConfigNew(ctx context.Context, cmd *base.Command, args []string) error { return err } +// printConfigOK outputs the confirmation message to the user. func printConfigOK(filename string) (n int, err error) { return fmt.Printf("Your new API limits config is ready: %q\n", filename) } @@ -99,7 +100,7 @@ func shouldOverwrite(filename string, override bool) bool { } // maybeFixExt checks if the extension is one of .yaml or .yml, and if not -// appends it to teh file. +// appends it to the file. func maybeFixExt(filename string) string { if ext := filepath.Ext(filename); !(ext == ".yaml" || ext == ".yml") { return maybeAppendExt(filename, ".yaml") diff --git a/cmd/slackdump/internal/apiconfig/schema.json b/cmd/slackdump/internal/apiconfig/schema.json index 67a46c88..0b8050ec 100644 --- a/cmd/slackdump/internal/apiconfig/schema.json +++ b/cmd/slackdump/internal/apiconfig/schema.json @@ -27,6 +27,9 @@ "tier_3": { "$ref": "#/definitions/Tier" }, + "tier_4": { + "$ref": "#/definitions/Tier" + }, "per_request": { "$ref": "#/definitions/PerRequest" } @@ -55,12 +58,12 @@ "description": "Number of replies to fetch per request", "minimum": 1, "maximum": 1000 - } }, "title": "PerRequest" }, "Tier": { + "title": "Tier", "type": "object", "additionalProperties": false, "description": "Rate limiting tier", @@ -78,8 +81,7 @@ "type": "integer", "description": "Number of times to retry a request" } - }, - "title": "Tier" + } } } } diff --git a/slackdump.1 b/slackdump.1 index 82cdd17a..37658b7c 100644 --- a/slackdump.1 +++ b/slackdump.1 @@ -53,9 +53,10 @@ Allows to perform different operations on the API limits configuration files. .It Cm convert Convert between formats. -.It Cm diag Ar subcommand -Contains various diagnostic utilities. Developers might ask to run -these commands to help with debugging. +.It Cm tools Ar subcommand +Contains various diagnostic and convenience utilities. Developers might ask +to run these commands to help with debugging. See TOOLS section for more +information. .It Cm dump Dump selected channels or threads. .It Cm emoji