232 lines
4.9 KiB
Go
232 lines
4.9 KiB
Go
package config
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestValidate(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
config *Config
|
|
strict bool
|
|
wantValid bool
|
|
wantErrors int
|
|
}{
|
|
{
|
|
name: "Valid config",
|
|
config: &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",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
strict: true,
|
|
wantValid: true,
|
|
wantErrors: 0,
|
|
},
|
|
{
|
|
name: "Missing name",
|
|
config: &Config{
|
|
Version: "1.0.0",
|
|
Description: "Test Application",
|
|
Author: "Test Author",
|
|
License: "MIT",
|
|
Build: BuildConfig{
|
|
OutputDir: "dist",
|
|
Targets: []string{"windows", "linux"},
|
|
Formats: []string{"msi", "deb"},
|
|
},
|
|
},
|
|
strict: true,
|
|
wantValid: false,
|
|
wantErrors: 1,
|
|
},
|
|
{
|
|
name: "Missing version",
|
|
config: &Config{
|
|
Name: "TestApp",
|
|
Description: "Test Application",
|
|
Author: "Test Author",
|
|
License: "MIT",
|
|
Build: BuildConfig{
|
|
OutputDir: "dist",
|
|
Targets: []string{"windows", "linux"},
|
|
Formats: []string{"msi", "deb"},
|
|
},
|
|
},
|
|
strict: true,
|
|
wantValid: false,
|
|
wantErrors: 1,
|
|
},
|
|
{
|
|
name: "Invalid target",
|
|
config: &Config{
|
|
Name: "TestApp",
|
|
Version: "1.0.0",
|
|
Description: "Test Application",
|
|
Author: "Test Author",
|
|
License: "MIT",
|
|
Build: BuildConfig{
|
|
OutputDir: "dist",
|
|
Targets: []string{"windows", "invalid"},
|
|
Formats: []string{"msi", "deb"},
|
|
},
|
|
},
|
|
strict: true,
|
|
wantValid: false,
|
|
wantErrors: 1,
|
|
},
|
|
{
|
|
name: "Invalid format",
|
|
config: &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", "invalid"},
|
|
},
|
|
},
|
|
strict: true,
|
|
wantValid: false,
|
|
wantErrors: 1,
|
|
},
|
|
{
|
|
name: "Missing optional fields in non-strict mode",
|
|
config: &Config{
|
|
Name: "TestApp",
|
|
Version: "1.0.0",
|
|
Build: BuildConfig{
|
|
Targets: []string{"windows"},
|
|
Formats: []string{"msi"},
|
|
},
|
|
},
|
|
strict: false,
|
|
wantValid: true,
|
|
wantErrors: 0,
|
|
},
|
|
{
|
|
name: "Missing optional fields in strict mode",
|
|
config: &Config{
|
|
Name: "TestApp",
|
|
Version: "1.0.0",
|
|
Build: BuildConfig{
|
|
Targets: []string{"windows"},
|
|
Formats: []string{"msi"},
|
|
},
|
|
},
|
|
strict: true,
|
|
wantValid: false,
|
|
wantErrors: 3, // 缺少描述、作者和许可证
|
|
},
|
|
{
|
|
name: "Invalid script type",
|
|
config: &Config{
|
|
Name: "TestApp",
|
|
Version: "1.0.0",
|
|
Description: "Test Application",
|
|
Author: "Test Author",
|
|
License: "MIT",
|
|
Build: BuildConfig{
|
|
OutputDir: "dist",
|
|
Targets: []string{"windows"},
|
|
Formats: []string{"msi"},
|
|
},
|
|
Contents: ContentsConfig{
|
|
Scripts: []ScriptConfig{
|
|
{
|
|
Type: "invalid",
|
|
Content: "echo 'Invalid script'",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
strict: true,
|
|
wantValid: false,
|
|
wantErrors: 1,
|
|
},
|
|
{
|
|
name: "Empty script",
|
|
config: &Config{
|
|
Name: "TestApp",
|
|
Version: "1.0.0",
|
|
Description: "Test Application",
|
|
Author: "Test Author",
|
|
License: "MIT",
|
|
Build: BuildConfig{
|
|
OutputDir: "dist",
|
|
Targets: []string{"windows"},
|
|
Formats: []string{"msi"},
|
|
},
|
|
Contents: ContentsConfig{
|
|
Scripts: []ScriptConfig{
|
|
{
|
|
Type: "preinstall",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
strict: true,
|
|
wantValid: false,
|
|
wantErrors: 1,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := tt.config.Validate(tt.strict)
|
|
assert.Equal(t, tt.wantValid, result.Valid)
|
|
assert.Equal(t, tt.wantErrors, len(result.Errors))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidationResult_String(t *testing.T) {
|
|
// 测试空错误列表
|
|
result := ValidationResult{
|
|
Valid: true,
|
|
Errors: []ValidationError{},
|
|
}
|
|
assert.Equal(t, "配置有效", result.String())
|
|
|
|
// 测试单个错误
|
|
result = ValidationResult{
|
|
Valid: false,
|
|
Errors: []ValidationError{
|
|
{Field: "test", Message: "错误1"},
|
|
},
|
|
}
|
|
assert.Equal(t, "配置无效: 错误1", result.String())
|
|
|
|
// 测试多个错误
|
|
result = ValidationResult{
|
|
Valid: false,
|
|
Errors: []ValidationError{
|
|
{Message: "错误1"},
|
|
{Message: "错误2"},
|
|
{Message: "错误3"},
|
|
},
|
|
}
|
|
assert.Equal(t, "配置无效: 错误1, 错误2, 错误3", result.String())
|
|
}
|