mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-07-08 11:31:57 +08:00
feat(i18n): 收口多语言功能业务代码
This commit is contained in:
@@ -2,7 +2,7 @@ package appdata
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -10,6 +10,51 @@ import (
|
||||
|
||||
const bootstrapFileName = "storage_root.json"
|
||||
|
||||
var (
|
||||
ErrSetActiveRootCreateDataDirectory = errors.New("create data directory failed")
|
||||
ErrSetActiveRootCreateBootstrapDirectory = errors.New("create bootstrap directory failed")
|
||||
)
|
||||
|
||||
type setActiveRootError struct {
|
||||
kind error
|
||||
detail error
|
||||
}
|
||||
|
||||
func (e *setActiveRootError) Error() string {
|
||||
if e == nil || e.kind == nil {
|
||||
return ""
|
||||
}
|
||||
if e.detail == nil {
|
||||
return e.kind.Error()
|
||||
}
|
||||
return e.kind.Error() + ": " + e.detail.Error()
|
||||
}
|
||||
|
||||
func (e *setActiveRootError) Unwrap() error {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return e.kind
|
||||
}
|
||||
|
||||
func newSetActiveRootError(kind error, detail error) error {
|
||||
if kind == nil {
|
||||
return detail
|
||||
}
|
||||
if detail == nil {
|
||||
return kind
|
||||
}
|
||||
return &setActiveRootError{kind: kind, detail: detail}
|
||||
}
|
||||
|
||||
func SetActiveRootErrorDetail(err error) error {
|
||||
var target *setActiveRootError
|
||||
if errors.As(err, &target) {
|
||||
return target.detail
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type bootstrapConfig struct {
|
||||
DataRoot string `json:"dataRoot"`
|
||||
}
|
||||
@@ -90,7 +135,7 @@ func SetActiveRoot(root string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
if err := os.MkdirAll(targetRoot, 0o755); err != nil {
|
||||
return "", fmt.Errorf("创建数据目录失败:%w", err)
|
||||
return "", newSetActiveRootError(ErrSetActiveRootCreateDataDirectory, err)
|
||||
}
|
||||
if targetRoot == defaultRoot {
|
||||
if err := os.Remove(BootstrapPath()); err != nil && !os.IsNotExist(err) {
|
||||
@@ -99,7 +144,7 @@ func SetActiveRoot(root string) (string, error) {
|
||||
return defaultRoot, nil
|
||||
}
|
||||
if err := os.MkdirAll(defaultRoot, 0o755); err != nil {
|
||||
return "", fmt.Errorf("创建默认引导目录失败:%w", err)
|
||||
return "", newSetActiveRootError(ErrSetActiveRootCreateBootstrapDirectory, err)
|
||||
}
|
||||
payload, err := json.MarshalIndent(bootstrapConfig{DataRoot: targetRoot}, "", " ")
|
||||
if err != nil {
|
||||
|
||||
53
internal/appdata/root_i18n_test.go
Normal file
53
internal/appdata/root_i18n_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package appdata
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSetActiveRootUsesCreateDataDirectorySentinel(t *testing.T) {
|
||||
homeDir := t.TempDir()
|
||||
t.Setenv("HOME", homeDir)
|
||||
t.Setenv("USERPROFILE", homeDir)
|
||||
|
||||
blockingPath := filepath.Join(t.TempDir(), "blocked-root")
|
||||
if err := os.WriteFile(blockingPath, []byte("blocked"), 0o644); err != nil {
|
||||
t.Fatalf("write blocking path: %v", err)
|
||||
}
|
||||
|
||||
_, err := SetActiveRoot(blockingPath)
|
||||
if err == nil {
|
||||
t.Fatal("expected SetActiveRoot to fail when target path is an existing file")
|
||||
}
|
||||
if !errors.Is(err, ErrSetActiveRootCreateDataDirectory) {
|
||||
t.Fatalf("expected create-data-directory sentinel, got %v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), "创建数据目录失败") {
|
||||
t.Fatalf("expected no raw Chinese create-data-directory wrapper, got %q", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetActiveRootUsesCreateBootstrapDirectorySentinel(t *testing.T) {
|
||||
homeDir := t.TempDir()
|
||||
t.Setenv("HOME", homeDir)
|
||||
t.Setenv("USERPROFILE", homeDir)
|
||||
|
||||
defaultRoot := filepath.Join(homeDir, ".gonavi")
|
||||
if err := os.WriteFile(defaultRoot, []byte("blocked"), 0o644); err != nil {
|
||||
t.Fatalf("write blocking default root: %v", err)
|
||||
}
|
||||
|
||||
_, err := SetActiveRoot(filepath.Join(t.TempDir(), "custom-root"))
|
||||
if err == nil {
|
||||
t.Fatal("expected SetActiveRoot to fail when default bootstrap directory is blocked by a file")
|
||||
}
|
||||
if !errors.Is(err, ErrSetActiveRootCreateBootstrapDirectory) {
|
||||
t.Fatalf("expected create-bootstrap-directory sentinel, got %v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), "创建默认引导目录失败") {
|
||||
t.Fatalf("expected no raw Chinese create-bootstrap-directory wrapper, got %q", err.Error())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user