-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
315 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,75 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/xhd2015/xgo/support/fileutil" | ||
) | ||
|
||
// TODO: use generate to ensure options sync | ||
// see patch/match/match.go, patch/ctxt/match_options.go | ||
|
||
type Rule struct { | ||
Any bool `json:"any"` | ||
Kind *string `json:"kind"` | ||
Pkg *string `json:"pkg"` | ||
Name *string `json:"name"` | ||
Stdlib *bool `json:"stdlib"` | ||
MainModule *bool `json:"main_module"` | ||
Generic *bool `json:"generic"` | ||
Exported *bool `json:"exported"` | ||
Action string `json:"action"` // include,exclude or empty | ||
} | ||
|
||
type FileOptions struct { | ||
FilterRules []Rule `json:"filter_rules"` | ||
} | ||
|
||
func mergeOptionFiles(tmpDir string, optionFromFile string, mockRules []string) (newFile string, content []byte, err error) { | ||
if len(mockRules) == 0 { | ||
if optionFromFile != "" { | ||
content, err = fileutil.ReadFile(optionFromFile) | ||
} | ||
return optionFromFile, content, err | ||
} | ||
var opts FileOptions | ||
if optionFromFile != "" { | ||
optionFromFileContent, err := fileutil.ReadFile(optionFromFile) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
|
||
if len(optionFromFileContent) > 0 { | ||
err := json.Unmarshal(optionFromFileContent, &opts) | ||
if err != nil { | ||
return "", nil, fmt.Errorf("parse %s: %w", optionFromFile, err) | ||
} | ||
} | ||
} | ||
|
||
rulesFromFiles := opts.FilterRules | ||
mergedRules := make([]Rule, 0, len(rulesFromFiles)+len(mockRules)) | ||
for _, mockRule := range mockRules { | ||
if mockRule == "" { | ||
continue | ||
} | ||
var rule Rule | ||
err := json.Unmarshal([]byte(mockRule), &rule) | ||
if err != nil { | ||
return "", nil, fmt.Errorf("parse mock rule: %s %w", mockRule, err) | ||
} | ||
mergedRules = append(mergedRules, rule) | ||
} | ||
|
||
mergedRules = append(mergedRules, rulesFromFiles...) | ||
|
||
newOptionFile, err := json.Marshal(FileOptions{FilterRules: mergedRules}) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
newFile = filepath.Join(tmpDir, "options-from-file.json") | ||
err = fileutil.WriteFile(newFile, newOptionFile) | ||
return newFile, newOptionFile, err | ||
} |
This file contains 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 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 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,5 @@ | ||
module github.com/xhd2015/xgo/cmd/xgo/test-explorer/testdata/mock_rules | ||
|
||
go 1.14 | ||
|
||
require github.com/xhd2015/xgo/runtime v1.0.43 |
This file contains 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,2 @@ | ||
github.com/xhd2015/xgo/runtime v1.0.43 h1:Nv8jJiboLQz3436f8DX7lEYZ2I90IkqcJY3UhXxRbAk= | ||
github.com/xhd2015/xgo/runtime v1.0.43/go.mod h1:9GBQ2SzJCzpD3T+HRN+2C0TUOGv7qIz4s0mad1xJ8Jo= |
34 changes: 34 additions & 0 deletions
34
cmd/xgo/test-explorer/testdata/mock_rules/mock_rules_test.go
This file contains 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 prog_arg | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/xhd2015/xgo/runtime/mock" | ||
) | ||
|
||
// run example: | ||
// | ||
// go run -tags dev ./cmd/xgo e --project-dir cmd/xgo/test-explorer/testdata/mock_rules | ||
func TestProgArg(t *testing.T) { | ||
var panicErr interface{} | ||
func() { | ||
defer func() { | ||
panicErr = recover() | ||
}() | ||
mock.Patch(http.Get, func(url string) (*http.Response, error) { | ||
return nil, nil | ||
}) | ||
}() | ||
|
||
if panicErr == nil { | ||
t.Fatalf("expect mock http.Get panic, actually not") | ||
} | ||
|
||
msg := fmt.Sprint(panicErr) | ||
expect := "failed to setup mock for: net/http.Get" | ||
if msg != expect { | ||
t.Fatalf("expect panic %q, actual: %q", expect, msg) | ||
} | ||
} |
This file contains 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,8 @@ | ||
{ | ||
"mock_rules": [ | ||
{ | ||
"stdlib": true, | ||
"action": "exclude" | ||
} | ||
] | ||
} |
This file contains 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
Oops, something went wrong.