255 lines
6.7 KiB
Go
255 lines
6.7 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestLoadConfig(t *testing.T) {
|
|
// 创建临时测试目录
|
|
tempDir, err := os.MkdirTemp("", "config-test-*")
|
|
require.NoError(t, err)
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
// 创建有效的YAML配置文件
|
|
validYAML := `
|
|
name: TestApp
|
|
version: 1.0.0
|
|
description: Test Application
|
|
author: Test Author
|
|
license: MIT
|
|
|
|
build:
|
|
outputDir: dist
|
|
targets:
|
|
- windows
|
|
- linux
|
|
formats:
|
|
- msi
|
|
- deb
|
|
|
|
contents:
|
|
files:
|
|
- source: test.txt
|
|
destination: test.txt
|
|
mode: "0644"
|
|
scripts:
|
|
- type: preinstall
|
|
content: "echo 'Pre-install script'"
|
|
- type: postinstall
|
|
source: scripts/post-install.sh
|
|
`
|
|
validYAMLPath := filepath.Join(tempDir, "valid.yaml")
|
|
err = os.WriteFile(validYAMLPath, []byte(validYAML), 0644)
|
|
require.NoError(t, err)
|
|
|
|
// 创建无效的YAML配置文件
|
|
invalidYAML := `
|
|
name: TestApp
|
|
version: 1.0.0
|
|
invalid_field: value
|
|
`
|
|
invalidYAMLPath := filepath.Join(tempDir, "invalid.yaml")
|
|
err = os.WriteFile(invalidYAMLPath, []byte(invalidYAML), 0644)
|
|
require.NoError(t, err)
|
|
|
|
// 创建有效的JSON配置文件
|
|
validJSON := `{
|
|
"name": "TestApp",
|
|
"version": "1.0.0",
|
|
"description": "Test Application",
|
|
"author": "Test Author",
|
|
"license": "MIT",
|
|
"build": {
|
|
"outputDir": "dist",
|
|
"targets": ["windows", "linux"],
|
|
"formats": ["msi", "deb"]
|
|
},
|
|
"contents": {
|
|
"files": [
|
|
{
|
|
"source": "test.txt",
|
|
"destination": "test.txt",
|
|
"mode": "0644"
|
|
}
|
|
],
|
|
"scripts": [
|
|
{
|
|
"type": "preinstall",
|
|
"content": "echo 'Pre-install script'"
|
|
},
|
|
{
|
|
"type": "postinstall",
|
|
"source": "scripts/post-install.sh"
|
|
}
|
|
]
|
|
}
|
|
}`
|
|
validJSONPath := filepath.Join(tempDir, "valid.json")
|
|
err = os.WriteFile(validJSONPath, []byte(validJSON), 0644)
|
|
require.NoError(t, err)
|
|
|
|
// 测试用例
|
|
tests := []struct {
|
|
name string
|
|
path string
|
|
wantErr bool
|
|
checkCfg func(*Config)
|
|
}{
|
|
{
|
|
name: "Valid YAML config",
|
|
path: validYAMLPath,
|
|
wantErr: false,
|
|
checkCfg: func(cfg *Config) {
|
|
assert.Equal(t, "TestApp", cfg.Name)
|
|
assert.Equal(t, "1.0.0", cfg.Version)
|
|
assert.Equal(t, "Test Application", cfg.Description)
|
|
assert.Equal(t, "Test Author", cfg.Author)
|
|
assert.Equal(t, "MIT", cfg.License)
|
|
assert.Equal(t, "dist", cfg.Build.OutputDir)
|
|
assert.Contains(t, cfg.Build.Targets, "windows")
|
|
assert.Contains(t, cfg.Build.Targets, "linux")
|
|
assert.Contains(t, cfg.Build.Formats, "msi")
|
|
assert.Contains(t, cfg.Build.Formats, "deb")
|
|
assert.Len(t, cfg.Contents.Files, 1)
|
|
assert.Equal(t, "test.txt", cfg.Contents.Files[0].Source)
|
|
assert.Equal(t, "test.txt", cfg.Contents.Files[0].Destination)
|
|
assert.Equal(t, "0644", cfg.Contents.Files[0].Mode)
|
|
assert.Len(t, cfg.Contents.Scripts, 2)
|
|
assert.Equal(t, "preinstall", cfg.Contents.Scripts[0].Type)
|
|
assert.Equal(t, "echo 'Pre-install script'", cfg.Contents.Scripts[0].Content)
|
|
assert.Equal(t, "postinstall", cfg.Contents.Scripts[1].Type)
|
|
assert.Equal(t, "scripts/post-install.sh", cfg.Contents.Scripts[1].Source)
|
|
},
|
|
},
|
|
{
|
|
name: "Valid JSON config",
|
|
path: validJSONPath,
|
|
wantErr: false,
|
|
checkCfg: func(cfg *Config) {
|
|
assert.Equal(t, "TestApp", cfg.Name)
|
|
assert.Equal(t, "1.0.0", cfg.Version)
|
|
assert.Equal(t, "Test Application", cfg.Description)
|
|
assert.Equal(t, "Test Author", cfg.Author)
|
|
assert.Equal(t, "MIT", cfg.License)
|
|
},
|
|
},
|
|
{
|
|
name: "Invalid YAML config",
|
|
path: invalidYAMLPath,
|
|
wantErr: false, // 不会报错,但会有警告
|
|
checkCfg: func(cfg *Config) {
|
|
assert.Equal(t, "TestApp", cfg.Name)
|
|
assert.Equal(t, "1.0.0", cfg.Version)
|
|
},
|
|
},
|
|
{
|
|
name: "Non-existent file",
|
|
path: filepath.Join(tempDir, "nonexistent.yaml"),
|
|
wantErr: true,
|
|
checkCfg: func(cfg *Config) {
|
|
assert.Nil(t, cfg)
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
cfg, err := LoadConfig(tt.path)
|
|
if tt.wantErr {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
tt.checkCfg(cfg)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSaveConfig(t *testing.T) {
|
|
// 创建临时测试目录
|
|
tempDir, err := os.MkdirTemp("", "config-test-*")
|
|
require.NoError(t, err)
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
// 创建测试配置
|
|
cfg := &Config{
|
|
Name: "TestApp",
|
|
Version: "1.0.0",
|
|
Description: "Test Application",
|
|
Author: "Test Author",
|
|
License: "MIT",
|
|
Build: BuildConfig{
|
|
OutputDir: "dist",
|
|
Targets: []string{"windows", "linux"},
|
|
Formats: []string{"msi", "deb"},
|
|
},
|
|
Contents: ContentsConfig{
|
|
Files: []FileConfig{
|
|
{
|
|
Source: "test.txt",
|
|
Destination: "test.txt",
|
|
Mode: "0644",
|
|
},
|
|
},
|
|
Scripts: []ScriptConfig{
|
|
{
|
|
Type: "preinstall",
|
|
Content: "echo 'Pre-install script'",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
// 测试YAML保存
|
|
yamlPath := filepath.Join(tempDir, "config.yaml")
|
|
err = SaveConfig(cfg, yamlPath)
|
|
assert.NoError(t, err)
|
|
|
|
// 验证保存的YAML文件
|
|
loadedCfg, err := LoadConfig(yamlPath)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, cfg.Name, loadedCfg.Name)
|
|
assert.Equal(t, cfg.Version, loadedCfg.Version)
|
|
assert.Equal(t, cfg.Description, loadedCfg.Description)
|
|
assert.Equal(t, cfg.Build.OutputDir, loadedCfg.Build.OutputDir)
|
|
assert.ElementsMatch(t, cfg.Build.Targets, loadedCfg.Build.Targets)
|
|
assert.ElementsMatch(t, cfg.Build.Formats, loadedCfg.Build.Formats)
|
|
assert.Len(t, loadedCfg.Contents.Files, 1)
|
|
assert.Equal(t, cfg.Contents.Files[0].Source, loadedCfg.Contents.Files[0].Source)
|
|
assert.Equal(t, cfg.Contents.Files[0].Destination, loadedCfg.Contents.Files[0].Destination)
|
|
assert.Equal(t, cfg.Contents.Files[0].Mode, loadedCfg.Contents.Files[0].Mode)
|
|
assert.Len(t, loadedCfg.Contents.Scripts, 1)
|
|
assert.Equal(t, cfg.Contents.Scripts[0].Type, loadedCfg.Contents.Scripts[0].Type)
|
|
assert.Equal(t, cfg.Contents.Scripts[0].Content, loadedCfg.Contents.Scripts[0].Content)
|
|
|
|
// 测试JSON保存
|
|
jsonPath := filepath.Join(tempDir, "config.json")
|
|
err = SaveConfig(cfg, jsonPath)
|
|
assert.NoError(t, err)
|
|
|
|
// 验证保存的JSON文件
|
|
loadedCfg, err = LoadConfig(jsonPath)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, cfg.Name, loadedCfg.Name)
|
|
assert.Equal(t, cfg.Version, loadedCfg.Version)
|
|
assert.Equal(t, cfg.Description, loadedCfg.Description)
|
|
|
|
// 测试无效路径
|
|
err = SaveConfig(cfg, "/invalid/path/config.yaml")
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestDefaultConfig(t *testing.T) {
|
|
cfg := DefaultConfig()
|
|
assert.NotEmpty(t, cfg.Name)
|
|
assert.NotEmpty(t, cfg.Version)
|
|
assert.NotEmpty(t, cfg.Build.OutputDir)
|
|
assert.NotEmpty(t, cfg.Build.Targets)
|
|
assert.NotEmpty(t, cfg.Build.Formats)
|
|
}
|