-
Notifications
You must be signed in to change notification settings - Fork 5
Select default emulator on first run #217
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
Open
anisaoshafi
wants to merge
16
commits into
main
Choose a base branch
from
drg-381-add-option-to-select-default-emulator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
af4f7f3
Select default emulator on first run
anisaoshafi 5ce5a73
Parallelize new tests
anisaoshafi fba3335
Remove error & rename message for default emulator
anisaoshafi cb621fc
Match single quotes in config & handle commented section in detectBlo…
anisaoshafi 7018421
Assert error from runLstk
anisaoshafi cfe2273
Move ParseEmulatorType to its domain package
anisaoshafi 7620257
Make switch_test.go tests table-driven
anisaoshafi 9739902
Strong-type --emulator, validate early
anisaoshafi b7d7801
Remove callback in startEmulator: handle in run.go
anisaoshafi 0f681da
Remove ParseEmulatorType in favor of ParseOptionalEmulatorType
anisaoshafi 1d627a9
Gentler message on changing configuration
anisaoshafi 2aa3fff
Avoid duplicating config content
anisaoshafi bbd7f69
Skip keyboard shortcuts for emulator selection
anisaoshafi 96aa343
Nits
anisaoshafi ef8a3a5
Split emulator-selected note into two message events: change configur…
anisaoshafi 9b1d670
Simplify: Remove --emulator flag on start
anisaoshafi 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,34 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "regexp" | ||
| ) | ||
|
|
||
| var typeLineRe = regexp.MustCompile(`type\s*=\s*["'](\w+)["']`) | ||
|
|
||
| // SetEmulatorType rewrites the emulator type in the config file and reloads. | ||
| // No-op if the requested type is already set. | ||
| func SetEmulatorType(to EmulatorType) error { | ||
| path := resolvedConfigPath() | ||
| if path == "" { | ||
| return fmt.Errorf("no config file loaded") | ||
| } | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read config file: %w", err) | ||
| } | ||
| m := typeLineRe.FindStringSubmatch(string(data)) | ||
| if m == nil { | ||
| return fmt.Errorf("no emulator type field found in config") | ||
| } | ||
| if EmulatorType(m[1]) == to { | ||
| return nil | ||
| } | ||
| updated := typeLineRe.ReplaceAllString(string(data), `type = "`+string(to)+`"`) | ||
| if err := os.WriteFile(path, []byte(updated), 0644); err != nil { | ||
| return fmt.Errorf("failed to write config file: %w", err) | ||
| } | ||
| return loadConfig(path) | ||
| } |
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,61 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/viper" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSetEmulatorType_WritesAndReloads(t *testing.T) { | ||
| dir := t.TempDir() | ||
| path := filepath.Join(dir, "config.toml") | ||
| require.NoError(t, os.WriteFile(path, []byte("[[containers]]\ntype = \"aws\"\nport = \"4566\"\n"), 0644)) | ||
| require.NoError(t, loadConfig(path)) | ||
| t.Cleanup(func() { viper.Reset() }) | ||
|
|
||
| require.NoError(t, SetEmulatorType(EmulatorSnowflake)) | ||
|
|
||
| got, err := os.ReadFile(path) | ||
| require.NoError(t, err) | ||
| assert.Contains(t, string(got), `type = "snowflake"`) | ||
| assert.NotContains(t, string(got), `type = "aws"`) | ||
|
|
||
| cfg, err := Get() | ||
| require.NoError(t, err) | ||
| require.Len(t, cfg.Containers, 1) | ||
| assert.Equal(t, EmulatorSnowflake, cfg.Containers[0].Type) | ||
| } | ||
|
|
||
| func TestSetEmulatorType_NoOpWhenSameEmulator(t *testing.T) { | ||
| dir := t.TempDir() | ||
| path := filepath.Join(dir, "config.toml") | ||
| content := "[[containers]]\ntype = \"aws\"\nport = \"4566\"\n" | ||
| require.NoError(t, os.WriteFile(path, []byte(content), 0644)) | ||
| require.NoError(t, loadConfig(path)) | ||
| t.Cleanup(func() { viper.Reset() }) | ||
|
|
||
| require.NoError(t, SetEmulatorType(EmulatorAWS)) | ||
|
|
||
| got, err := os.ReadFile(path) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, content, string(got)) | ||
| } | ||
|
|
||
| func TestSetEmulatorType_PreservesInlineComments(t *testing.T) { | ||
| dir := t.TempDir() | ||
| path := filepath.Join(dir, "config.toml") | ||
| content := "[[containers]]\ntype = \"aws\" # Emulator type\ntag = \"latest\"\n" | ||
| require.NoError(t, os.WriteFile(path, []byte(content), 0644)) | ||
| require.NoError(t, loadConfig(path)) | ||
| t.Cleanup(func() { viper.Reset() }) | ||
|
|
||
| require.NoError(t, SetEmulatorType(EmulatorSnowflake)) | ||
|
|
||
| got, err := os.ReadFile(path) | ||
| require.NoError(t, err) | ||
| assert.Contains(t, string(got), `type = "snowflake" # Emulator type`) | ||
| } | ||
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.
nit: these tests seem to be a good fit for table-driven tests, WDYT?
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.
great observation, done: abc89ac