Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 43
Configurable commands #117
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "flag" | ||
| "fmt" | ||
| . "github.com/smartystreets/goconvey/convey" | ||
| "gopkg.in/sensorbee/sensorbee.v0/version" | ||
| "gopkg.in/urfave/cli.v1" | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestLoadConfig(t *testing.T) { | ||
| dir, err := ioutil.TempDir("", "build_sensorbee_load_config_test") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer os.RemoveAll(dir) | ||
|
|
||
| Convey("Given build_sensorbee tool", t, func() { | ||
| Convey("When load config with plugin and commands", func() { | ||
| cfgstr := `plugins: | ||
| - path/to/plugin | ||
| commands: | ||
| run: | ||
| runfile: | ||
| repo1: | ||
| path: path/to/repo | ||
| repo2: | ||
| path: path/to/repo2.v1 | ||
| ` | ||
| confName := filepath.Join(dir, "build_test.yaml") | ||
| So(ioutil.WriteFile(confName, []byte(cfgstr), 0644), ShouldBeNil) | ||
| Convey("Then the tool should load the config file", func() { | ||
| conf, err := loadConfig(confName) | ||
| So(err, ShouldBeNil) | ||
| expectedConf := Config{ | ||
| PluginPaths: []string{"path/to/plugin"}, | ||
| SubCommands: map[string]commandDetail{ | ||
| "run": commandDetail{}, | ||
| "runfile": commandDetail{}, | ||
| "repo1": commandDetail{Path: "path/to/repo"}, | ||
| "repo2": commandDetail{Path: "path/to/repo2.v1"}, | ||
| }, | ||
| Version: version.Version, | ||
| } | ||
| So(*conf, ShouldResemble, expectedConf) | ||
| }) | ||
| }) | ||
|
|
||
| Convey("When load config with empty yaml", func() { | ||
| confName := filepath.Join(dir, "build_test_empty.yaml") | ||
| So(ioutil.WriteFile(confName, []byte(""), 0644), ShouldBeNil) | ||
| Convey("Then the tool should load the config file", func() { | ||
| conf, err := loadConfig(confName) | ||
| So(err, ShouldBeNil) | ||
| expectedConf := Config{ | ||
| PluginPaths: []string(nil), | ||
| SubCommands: map[string]commandDetail{ | ||
| "run": commandDetail{}, | ||
| "shell": commandDetail{}, | ||
| "topology": commandDetail{}, | ||
| "exp": commandDetail{}, | ||
| "runfile": commandDetail{}, | ||
| }, | ||
| Version: version.Version, | ||
| } | ||
| So(*conf, ShouldResemble, expectedConf) | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| func TestCreateMainFile(t *testing.T) { | ||
| dir, err := ioutil.TempDir("", "build_sensorbee_create_test") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer os.RemoveAll(dir) | ||
| outFilename := filepath.Join(dir, "test_main.go") | ||
|
|
||
| set := flag.NewFlagSet("dummy", flag.ExitOnError) | ||
| set.String("source-filename", outFilename, "") | ||
| c := cli.NewContext(cli.NewApp(), set, nil) | ||
|
|
||
| Convey("Given build_sensorbee tool", t, func() { | ||
| Convey("When create a main file with a plugin and a buildin command", func() { | ||
| config := &Config{ | ||
| PluginPaths: []string{"path/to/plugin"}, | ||
| SubCommands: map[string]commandDetail{ | ||
| "run": commandDetail{}, | ||
| }, | ||
| Version: version.Version, | ||
| } | ||
| So(create(c, config), ShouldBeNil) | ||
| Convey("Then the main file should be created", func() { | ||
| b, err := ioutil.ReadFile(outFilename) | ||
| So(err, ShouldBeNil) | ||
| expectedMainFile := `package main | ||
|
|
||
| import ( | ||
| _ "gopkg.in/sensorbee/sensorbee.v0/bql/udf/builtin" | ||
| "gopkg.in/sensorbee/sensorbee.v0/cmd/lib/run" | ||
| "gopkg.in/sensorbee/sensorbee.v0/version" | ||
| "gopkg.in/urfave/cli.v1" | ||
| "os" | ||
| _ "path/to/plugin" | ||
| "time" | ||
| ) | ||
|
|
||
| func init() { | ||
| // TODO | ||
| time.Local = time.UTC | ||
| } | ||
|
|
||
| func main() { | ||
| app := cli.NewApp() | ||
| app.Name = "sensorbee" | ||
| app.Usage = "SensorBee built with build_sensorbee ` + version.Version + `" | ||
| app.Version = version.Version | ||
| app.Commands = []cli.Command{ | ||
| run.SetUp(), | ||
| } | ||
| if err := app.Run(os.Args); err != nil { | ||
| os.Exit(1) | ||
| } | ||
| } | ||
| ` | ||
| So(string(b), ShouldEqual, expectedMainFile) | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| func TestCreateMainFileWithCommandConfig(t *testing.T) { | ||
| dir, err := ioutil.TempDir("", "build_sensorbee_create_command_test") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer os.RemoveAll(dir) | ||
| outFilename := filepath.Join(dir, "test_main.go") | ||
|
|
||
| set := flag.NewFlagSet("dummy", flag.ExitOnError) | ||
| set.String("source-filename", outFilename, "") | ||
| c := cli.NewContext(cli.NewApp(), set, nil) | ||
|
|
||
| Convey("Given command configurations", t, func() { | ||
| type testCase struct { | ||
| title string | ||
| command map[string]commandDetail | ||
| expectedImport string | ||
| expectedSetup string | ||
| } | ||
| cases := []testCase{ | ||
| testCase{ | ||
| title: "build-in command", | ||
| command: map[string]commandDetail{"topology": commandDetail{}}, | ||
| expectedImport: `"gopkg.in/sensorbee/sensorbee.v0/cmd/lib/topology"`, | ||
| expectedSetup: "topology.SetUp(),", | ||
| }, | ||
| testCase{ | ||
| title: "custom command", | ||
| command: map[string]commandDetail{ | ||
| "repo1": commandDetail{Path: "path/to/repo.v1"}}, | ||
| expectedImport: `repo1 "path/to/repo.v1"`, | ||
| expectedSetup: "repo1.SetUp(),", | ||
| }, | ||
| } | ||
| for _, tc := range cases { | ||
| Convey(fmt.Sprintf("When create a main file with %s", tc.title), func() { | ||
| config := &Config{ | ||
| SubCommands: tc.command, | ||
| } | ||
| So(create(c, config), ShouldBeNil) | ||
| Convey("Then the main file should be created", func() { | ||
| b, err := ioutil.ReadFile(outFilename) | ||
| So(err, ShouldBeNil) | ||
| So(string(b), ShouldContainSubstring, tc.expectedImport) | ||
| So(string(b), ShouldContainSubstring, tc.expectedSetup) | ||
| }) | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
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.
You can’t perform that action at this time.

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.
How about adding a test using multiple
SubCommands? It doesn't have to perform an exact match of the contents but only needs to look up substrings likerepo1 "path/to/repo"(possibly with regexp) to see if all subcommands are correctly imported.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.
I separate to 2 test cases.
1st., a created main file is exactly match or not when configuration includes a plugin and a command.
2nd., a created main file has custom-command string or not with command configurations.