-
Notifications
You must be signed in to change notification settings - Fork 8
chore: parameterize SubCommandTest with configLevel #239
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
Changes from 23 commits
b30e994
d53fe2e
01afc5b
5c1405a
2340f19
f587286
b38b9b3
9217c44
ec0b84c
dae5016
5b5713f
bff221b
d7792bb
410be3a
9bfe476
a5975be
7fa5be7
e69744f
eda2623
ed293e7
69eaa04
139ef61
1512cd5
b7a3289
643f102
5957bdf
0336048
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ import ( | |
|
|
||
| tea "github.com/charmbracelet/bubbletea" | ||
| "github.com/columnar-tech/dbc" | ||
| "github.com/columnar-tech/dbc/config" | ||
| "github.com/go-faster/yaml" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
@@ -83,24 +84,26 @@ type SubcommandTestSuite struct { | |
| openBrowserFn func(string) error | ||
| fallbackDriverDocsUrl map[string]string | ||
| tempdir string | ||
|
|
||
| configLevel config.ConfigLevel | ||
| } | ||
|
|
||
| func (suite *SubcommandTestSuite) SetupSuite() { | ||
| suite.getDriverRegistryFn = getDriverRegistry | ||
| getDriverRegistry = getTestDriverRegistry | ||
| suite.openBrowserFn = openBrowserFunc | ||
| suite.fallbackDriverDocsUrl = fallbackDriverDocsUrl | ||
|
|
||
| if suite.configLevel == config.ConfigUnknown { | ||
| suite.configLevel = config.ConfigEnv | ||
| } | ||
| } | ||
|
|
||
| func (suite *SubcommandTestSuite) SetupTest() { | ||
| suite.tempdir = suite.T().TempDir() | ||
| suite.Require().NoError(os.Setenv("ADBC_DRIVER_PATH", suite.tempdir)) | ||
|
||
| } | ||
|
|
||
| func (suite *SubcommandTestSuite) TearDownTest() { | ||
| suite.Require().NoError(os.Unsetenv("ADBC_DRIVER_PATH")) | ||
| } | ||
|
Comment on lines
100
to
102
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why remove this? If we're not going to remove this env var after each test, then we should move the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This got split out into platform specific TearDownTest methods (subcommand_unix_test.go, subcommand_windows_test.go) |
||
|
|
||
| func (suite *SubcommandTestSuite) TearDownSuite() { | ||
| getDriverRegistry = suite.getDriverRegistryFn | ||
| openBrowserFunc = suite.openBrowserFn | ||
|
|
@@ -120,6 +123,15 @@ func (suite *SubcommandTestSuite) getFilesInTempDir() []string { | |
| return filelist | ||
| } | ||
|
|
||
| // Get the base directory for where drivers are installed. Use this instead of | ||
| // hardcoding checks to suite.tempdir to make tests support other config levels. | ||
| func (suite *SubcommandTestSuite) Dir() string { | ||
| if suite.configLevel == config.ConfigEnv { | ||
| return suite.tempdir | ||
| } | ||
| return config.GetLocation(suite.configLevel) | ||
| } | ||
|
|
||
| func (suite *SubcommandTestSuite) runCmdErr(m tea.Model) string { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
|
|
@@ -171,6 +183,64 @@ func (suite *SubcommandTestSuite) validateOutput(expected, extra, actual string) | |
| suite.Equal(terminalPrefix+expected+terminalSuffix+extra, actual) | ||
| } | ||
|
|
||
| func TestSubcommands(t *testing.T) { | ||
| suite.Run(t, new(SubcommandTestSuite)) | ||
| // The SubcommandTestSuite is only run for ConfigEnv by default but is | ||
| // parametrized by configLevel so tests can be run for other levels. Tests must | ||
| // opt into this behavior by instantiating subcommands with `suite.configLevel` | ||
| // like: | ||
| // | ||
| // m := InstallCmd{Driver: "foo", Level: suite.configLevel} | ||
| // ^---- here | ||
| // | ||
| // and can opt out of this behavior by specifying it separately like: | ||
| // | ||
| // m := InstallCmd{Driver: "test-driver-1", Level: config.ConfigEnv}. | ||
| // | ||
| // When any level is explicitly requested, tests are only run for that level. | ||
| // i.e., to run tests for multiple levels, each level must be specified | ||
| // separately. | ||
| func TestSubcommandsEnv(t *testing.T) { | ||
| _, env := os.LookupEnv("DBC_TEST_LEVEL_ENV") | ||
| _, user := os.LookupEnv("DBC_TEST_LEVEL_USER") | ||
| _, system := os.LookupEnv("DBC_TEST_LEVEL_SYSTEM") | ||
|
|
||
| // Run if explicitly requested, or if no levels were requested (default | ||
| // behavior) | ||
| if env || (!user && !system) { | ||
| suite.Run(t, &SubcommandTestSuite{configLevel: config.ConfigEnv}) | ||
| return | ||
| } | ||
| t.Skip("skipping tests for config level: ConfigEnv") | ||
| } | ||
|
|
||
| func TestSubcommandsUser(t *testing.T) { | ||
| if _, ok := os.LookupEnv("DBC_TEST_LEVEL_USER"); !ok { | ||
| t.Skip("skipping tests for config level: ConfigUser") | ||
| } | ||
| suite.Run(t, &SubcommandTestSuite{configLevel: config.ConfigUser}) | ||
| } | ||
|
|
||
| func TestSubcommandsSystem(t *testing.T) { | ||
| if _, ok := os.LookupEnv("DBC_TEST_LEVEL_SYSTEM"); !ok { | ||
| t.Skip("skipping tests for config level: ConfigSystem") | ||
| } | ||
| suite.Run(t, &SubcommandTestSuite{configLevel: config.ConfigSystem}) | ||
| } | ||
|
|
||
| func (suite *SubcommandTestSuite) driverIsInstalled(path string, checkShared bool) { | ||
| cfg := config.Get()[suite.configLevel] | ||
|
|
||
| driver, err := config.GetDriver(cfg, path) | ||
| suite.Require().NoError(err, "driver manifest should exist for driver `%s`", path) | ||
|
|
||
| if checkShared { | ||
| sharedPath := driver.Driver.Shared.Get(config.PlatformTuple()) | ||
| suite.FileExists(sharedPath, "driver shared library should exist for driver `%s`", path) | ||
| } | ||
| } | ||
|
|
||
| func (suite *SubcommandTestSuite) driverIsNotInstalled(path string) { | ||
| cfg := config.Get()[suite.configLevel] | ||
|
|
||
| _, err := config.GetDriver(cfg, path) | ||
| suite.Require().Error(err, "driver manifest should not exist for driver `%s`", path) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright 2025 Columnar Technologies Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //go:build !windows | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
|
|
||
| "github.com/columnar-tech/dbc/config" | ||
| ) | ||
|
|
||
| func (suite *SubcommandTestSuite) TearDownTest() { | ||
| suite.Require().NoError(os.Unsetenv("ADBC_DRIVER_PATH")) | ||
|
|
||
| // Clean up filesystem after each test | ||
| _, user := os.LookupEnv("DBC_TEST_LEVEL_USER") | ||
| _, system := os.LookupEnv("DBC_TEST_LEVEL_SYSTEM") | ||
| if user { | ||
| suite.Require().NoError(os.RemoveAll(config.GetLocation(config.ConfigUser))) | ||
| } | ||
| if system { | ||
| suite.Require().NoError(os.RemoveAll(config.GetLocation(config.ConfigSystem))) | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.