installerbuilder/internal/builder/windows/msi_test.go

205 lines
5.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package windows
import (
"os"
"path/filepath"
"testing"
"git.kingecg.top/kingecg/installerbuilder/internal/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMSIBuilder_Name(t *testing.T) {
cfg := &config.Config{Name: "TestApp", Version: "1.0.0"}
builder := NewMSIBuilder(cfg, "output")
assert.Equal(t, "Windows MSI Builder", builder.Name())
}
func TestMSIBuilder_SupportedPlatforms(t *testing.T) {
cfg := &config.Config{Name: "TestApp", Version: "1.0.0"}
builder := NewMSIBuilder(cfg, "output")
platforms := builder.SupportedPlatforms()
assert.Equal(t, []string{"windows"}, platforms)
}
func TestMSIBuilder_SupportedFormats(t *testing.T) {
cfg := &config.Config{Name: "TestApp", Version: "1.0.0"}
builder := NewMSIBuilder(cfg, "output")
formats := builder.SupportedFormats()
assert.Equal(t, []string{"msi"}, formats)
}
func TestMSIBuilder_CreateWixSource(t *testing.T) {
// 创建临时测试目录
tempDir, err := os.MkdirTemp("", "msi-builder-test-*")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
// 创建测试配置
cfg := &config.Config{
Name: "TestApp",
Version: "1.0.0",
Description: "Test Application",
Author: "Test Author",
License: "MIT",
Platforms: config.PlatformsConfig{
Windows: config.WindowsConfig{
Msi: config.WindowsMsiConfig{
Manufacturer: "Test Manufacturer",
UpgradeCode: "12345678-1234-1234-1234-123456789012",
InstallDir: "TestApp",
},
},
},
}
// 创建MSI构建器
builder := NewMSIBuilder(cfg, tempDir)
// 测试创建WiX源文件
wixFile := filepath.Join(tempDir, "installer.wxs")
err = builder.createWixSource(wixFile)
assert.NoError(t, err)
// 验证文件是否存在
_, err = os.Stat(wixFile)
assert.NoError(t, err)
// 读取文件内容
content, err := os.ReadFile(wixFile)
assert.NoError(t, err)
// 验证文件内容
contentStr := string(content)
assert.Contains(t, contentStr, `Name="TestApp"`)
assert.Contains(t, contentStr, `Version="1.0.0"`)
assert.Contains(t, contentStr, `Manufacturer="Test Manufacturer"`)
assert.Contains(t, contentStr, `UpgradeCode="12345678-1234-1234-1234-123456789012"`)
assert.Contains(t, contentStr, `Description="Test Application"`)
assert.Contains(t, contentStr, `Name="TestApp"`)
}
func TestMSIBuilder_Build(t *testing.T) {
// 跳过实际构建测试因为它需要WiX工具集
t.Skip("跳过实际构建测试因为它需要WiX工具集")
// 创建临时测试目录
tempDir, err := os.MkdirTemp("", "msi-builder-test-*")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
// 创建输出目录
outputDir := filepath.Join(tempDir, "output")
err = os.MkdirAll(outputDir, 0755)
require.NoError(t, err)
// 创建测试配置
cfg := &config.Config{
Name: "TestApp",
Version: "1.0.0",
Description: "Test Application",
Author: "Test Author",
License: "MIT",
Platforms: config.PlatformsConfig{
Windows: config.WindowsConfig{
Msi: config.WindowsMsiConfig{
Manufacturer: "Test Manufacturer",
UpgradeCode: "12345678-1234-1234-1234-123456789012",
InstallDir: "TestApp",
},
},
},
}
// 创建MSI构建器
builder := NewMSIBuilder(cfg, outputDir)
// 测试构建
err = builder.Build()
assert.NoError(t, err)
// 验证输出文件是否存在
outputFile := filepath.Join(outputDir, "TestApp-1.0.0.msi")
_, err = os.Stat(outputFile)
assert.NoError(t, err)
}
func TestMSIBuilder_CompileWixSource(t *testing.T) {
// 跳过实际编译测试因为它需要WiX工具集
t.Skip("跳过实际编译测试因为它需要WiX工具集")
// 创建临时测试目录
tempDir, err := os.MkdirTemp("", "msi-builder-test-*")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
// 创建测试配置
cfg := &config.Config{
Name: "TestApp",
Version: "1.0.0",
Description: "Test Application",
}
// 创建MSI构建器
builder := NewMSIBuilder(cfg, tempDir)
// 创建WiX源文件
wixFile := filepath.Join(tempDir, "installer.wxs")
err = builder.createWixSource(wixFile)
assert.NoError(t, err)
// 测试编译WiX源文件
objFile := filepath.Join(tempDir, "installer.wixobj")
err = builder.compileWixSource(wixFile, objFile)
assert.NoError(t, err)
// 验证输出文件是否存在
_, err = os.Stat(objFile)
assert.NoError(t, err)
}
func TestMSIBuilder_LinkMsi(t *testing.T) {
// 跳过实际链接测试因为它需要WiX工具集
t.Skip("跳过实际链接测试因为它需要WiX工具集")
// 创建临时测试目录
tempDir, err := os.MkdirTemp("", "msi-builder-test-*")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
// 创建输出目录
outputDir := filepath.Join(tempDir, "output")
err = os.MkdirAll(outputDir, 0755)
require.NoError(t, err)
// 创建测试配置
cfg := &config.Config{
Name: "TestApp",
Version: "1.0.0",
Description: "Test Application",
}
// 创建MSI构建器
builder := NewMSIBuilder(cfg, outputDir)
// 创建WiX源文件
wixFile := filepath.Join(tempDir, "installer.wxs")
err = builder.createWixSource(wixFile)
assert.NoError(t, err)
// 编译WiX源文件
objFile := filepath.Join(tempDir, "installer.wixobj")
err = builder.compileWixSource(wixFile, objFile)
assert.NoError(t, err)
// 测试链接生成MSI
msiFile := filepath.Join(outputDir, "TestApp-1.0.0.msi")
err = builder.linkMsi(objFile, msiFile)
assert.NoError(t, err)
// 验证输出文件是否存在
_, err = os.Stat(msiFile)
assert.NoError(t, err)
}