mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 07:10:40 +08:00
Fixed an error that occurred when an author name contains a string that is not suitable for JSON (#3638)
* fix(pkg): Fixed an issue where wails new would throw an error if the author name contained non-JSON legal characters. * refactor(pkg): Incorporating coderabbit's suggestions * docs: write changelog.mdx * Escape using json package. Add tests. * Update test. --------- Co-authored-by: Lea O'Anthony <lea.anthony@gmail.com>
This commit is contained in:
parent
fe9495d776
commit
5b091db26d
@ -1,7 +1,8 @@
|
|||||||
package git
|
package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -30,9 +31,31 @@ func Email() (string, error) {
|
|||||||
|
|
||||||
// Name tries to retrieve the
|
// Name tries to retrieve the
|
||||||
func Name() (string, error) {
|
func Name() (string, error) {
|
||||||
|
errMsg := "failed to retrieve git user name: %w"
|
||||||
stdout, _, err := shell.RunCommand(".", gitcommand(), "config", "user.name")
|
stdout, _, err := shell.RunCommand(".", gitcommand(), "config", "user.name")
|
||||||
name := template.JSEscapeString(strings.TrimSpace(stdout))
|
if err != nil {
|
||||||
return name, err
|
return "", fmt.Errorf(errMsg, err)
|
||||||
|
}
|
||||||
|
name := strings.TrimSpace(stdout)
|
||||||
|
return EscapeName(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EscapeName(str string) (string, error) {
|
||||||
|
b, err := json.Marshal(str)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
// Remove the surrounding quotes
|
||||||
|
escaped := string(b[1 : len(b)-1])
|
||||||
|
|
||||||
|
// Check if username is JSON compliant
|
||||||
|
var js json.RawMessage
|
||||||
|
jsonVal := fmt.Sprintf(`{"name": "%s"}`, escaped)
|
||||||
|
err = json.Unmarshal([]byte(jsonVal), &js)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to retrieve git user name: %w", err)
|
||||||
|
}
|
||||||
|
return escaped, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitRepo(projectDir string) error {
|
func InitRepo(projectDir string) error {
|
||||||
|
44
v2/pkg/git/git_test.go
Normal file
44
v2/pkg/git/git_test.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package git
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEscapeName1(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
str string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Escape Apostrophe",
|
||||||
|
args: args{
|
||||||
|
str: `John O'Keefe`,
|
||||||
|
},
|
||||||
|
want: `John O'Keefe`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Escape backslash",
|
||||||
|
args: args{
|
||||||
|
str: `MYDOMAIN\USER`,
|
||||||
|
},
|
||||||
|
want: `MYDOMAIN\\USER`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := EscapeName(tt.args.str)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("EscapeName() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("EscapeName() got = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
- Fixed an error that occurred when an author name contains a string that is not suitable for JSON. Fixed by @taiseiotsuka in [PR](https://github.com/wailsapp/wails/pull/3638)
|
||||||
- Fixed MacOS build to use `outputfilename` from wails.json. [#3200](https://github.com/wailsapp/wails/issues/3200)
|
- Fixed MacOS build to use `outputfilename` from wails.json. [#3200](https://github.com/wailsapp/wails/issues/3200)
|
||||||
- Fixed file drop events on windows. Fixed in [PR](https://github.com/wailsapp/wails/pull/3595) by @FrancescoLuzzi
|
- Fixed file drop events on windows. Fixed in [PR](https://github.com/wailsapp/wails/pull/3595) by @FrancescoLuzzi
|
||||||
+ Fixed file drop events on Windows in [PR](https://github.com/wailsapp/wails/pull/3595) by @FrancescoLuzzi
|
+ Fixed file drop events on Windows in [PR](https://github.com/wailsapp/wails/pull/3595) by @FrancescoLuzzi
|
||||||
|
Loading…
Reference in New Issue
Block a user