|
| 1 | +package component_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "testing/fstest" |
| 7 | + |
| 8 | + "github.com/MakeNowJust/heredoc" |
| 9 | + "github.com/anchordotdev/cli" |
| 10 | + "github.com/anchordotdev/cli/clitest" |
| 11 | + "github.com/anchordotdev/cli/cmdtest" |
| 12 | + "github.com/anchordotdev/cli/component/models" |
| 13 | + "github.com/anchordotdev/cli/ui" |
| 14 | + "github.com/anchordotdev/cli/ui/uitest" |
| 15 | +) |
| 16 | + |
| 17 | +func TestConfigVia(t *testing.T) { |
| 18 | + |
| 19 | + cmd := configViaCommand{} |
| 20 | + |
| 21 | + t.Run("env", func(t *testing.T) { |
| 22 | + ctx, cancel := context.WithCancel(context.Background()) |
| 23 | + defer cancel() |
| 24 | + |
| 25 | + t.Setenv("ORG", "env-org") |
| 26 | + cfg := cmdtest.Config(ctx) |
| 27 | + ctx = cli.ContextWithConfig(ctx, cfg) |
| 28 | + |
| 29 | + uitest.TestTUIOutput(ctx, t, cmd.UI()) |
| 30 | + }) |
| 31 | + |
| 32 | + t.Run("toml", func(t *testing.T) { |
| 33 | + ctx, cancel := context.WithCancel(context.Background()) |
| 34 | + defer cancel() |
| 35 | + |
| 36 | + cfg := new(cli.Config) |
| 37 | + cfg.Test.SystemFS = clitest.TestFS{ |
| 38 | + "anchor.toml": &fstest.MapFile{ |
| 39 | + Data: []byte(heredoc.Doc(` |
| 40 | + [org] |
| 41 | + apid = "toml-org" |
| 42 | + `)), |
| 43 | + }, |
| 44 | + } |
| 45 | + if err := cfg.Load(ctx); err != nil { |
| 46 | + panic(err) |
| 47 | + } |
| 48 | + ctx = cli.ContextWithConfig(ctx, cfg) |
| 49 | + |
| 50 | + uitest.TestTUIOutput(ctx, t, cmd.UI()) |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("default", func(t *testing.T) { |
| 54 | + ctx, cancel := context.WithCancel(context.Background()) |
| 55 | + defer cancel() |
| 56 | + |
| 57 | + cfg := cmdtest.Config(ctx) |
| 58 | + ctx = cli.ContextWithConfig(ctx, cfg) |
| 59 | + |
| 60 | + uitest.TestTUIOutput(ctx, t, cmd.UI()) |
| 61 | + }) |
| 62 | + |
| 63 | + t.Run("flag", func(t *testing.T) { |
| 64 | + ctx, cancel := context.WithCancel(context.Background()) |
| 65 | + defer cancel() |
| 66 | + |
| 67 | + cfg := cmdtest.Config(ctx) |
| 68 | + cfg.Org.APID = "flag-org" |
| 69 | + ctx = cli.ContextWithConfig(ctx, cfg) |
| 70 | + |
| 71 | + uitest.TestTUIOutput(ctx, t, cmd.UI()) |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +type configViaCommand struct{} |
| 76 | + |
| 77 | +func (c configViaCommand) UI() cli.UI { |
| 78 | + return cli.UI{ |
| 79 | + RunTUI: c.run, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func (*configViaCommand) run(ctx context.Context, drv *ui.Driver) error { |
| 84 | + cfg := cli.ConfigFromContext(ctx) |
| 85 | + |
| 86 | + if cfg.Org.APID != "" { |
| 87 | + drv.Activate(ctx, &models.ConfigVia{ |
| 88 | + Config: cfg, |
| 89 | + ConfigFetchFn: func(cfg *cli.Config) any { return cfg.Org.APID }, |
| 90 | + Flag: "--org", |
| 91 | + Singular: "organization", |
| 92 | + }) |
| 93 | + return nil |
| 94 | + } |
| 95 | + if cfg.API.URL != "" { |
| 96 | + drv.Activate(ctx, &models.ConfigVia{ |
| 97 | + Config: cfg, |
| 98 | + ConfigFetchFn: func(cfg *cli.Config) any { return cfg.API.URL }, |
| 99 | + Flag: "API_URL=", |
| 100 | + Singular: "api url", |
| 101 | + }) |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
0 commit comments