297 lines
7.9 KiB
Go
297 lines
7.9 KiB
Go
package linux
|
||
|
||
import (
|
||
"os"
|
||
"path/filepath"
|
||
"testing"
|
||
|
||
"git.kingecg.top/kingecg/installerbuilder/internal/config"
|
||
"github.com/stretchr/testify/assert"
|
||
"github.com/stretchr/testify/require"
|
||
)
|
||
|
||
func TestDEBBuilder_Name(t *testing.T) {
|
||
cfg := &config.Config{Name: "TestApp", Version: "1.0.0"}
|
||
builder := NewDEBBuilder(cfg, "output")
|
||
assert.Equal(t, "Linux DEB Builder", builder.Name())
|
||
}
|
||
|
||
func TestDEBBuilder_SupportedPlatforms(t *testing.T) {
|
||
cfg := &config.Config{Name: "TestApp", Version: "1.0.0"}
|
||
builder := NewDEBBuilder(cfg, "output")
|
||
platforms := builder.SupportedPlatforms()
|
||
assert.Equal(t, []string{"linux"}, platforms)
|
||
}
|
||
|
||
func TestDEBBuilder_SupportedFormats(t *testing.T) {
|
||
cfg := &config.Config{Name: "TestApp", Version: "1.0.0"}
|
||
builder := NewDEBBuilder(cfg, "output")
|
||
formats := builder.SupportedFormats()
|
||
assert.Equal(t, []string{"deb"}, formats)
|
||
}
|
||
|
||
func TestDEBBuilder_CreateDebStructure(t *testing.T) {
|
||
// 创建临时测试目录
|
||
tempDir, err := os.MkdirTemp("", "deb-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",
|
||
}
|
||
|
||
// 创建DEB构建器
|
||
builder := NewDEBBuilder(cfg, tempDir)
|
||
|
||
// 测试创建DEB包结构
|
||
err = builder.createDebStructure(tempDir)
|
||
assert.NoError(t, err)
|
||
|
||
// 验证目录结构
|
||
debianDir := filepath.Join(tempDir, "DEBIAN")
|
||
_, err = os.Stat(debianDir)
|
||
assert.NoError(t, err)
|
||
|
||
installDir := filepath.Join(tempDir, "usr", "local", "bin")
|
||
_, err = os.Stat(installDir)
|
||
assert.NoError(t, err)
|
||
|
||
docDir := filepath.Join(tempDir, "usr", "share", "doc", "TestApp")
|
||
_, err = os.Stat(docDir)
|
||
assert.NoError(t, err)
|
||
}
|
||
|
||
func TestDEBBuilder_CreateControlFile(t *testing.T) {
|
||
// 创建临时测试目录
|
||
tempDir, err := os.MkdirTemp("", "deb-builder-test-*")
|
||
require.NoError(t, err)
|
||
defer os.RemoveAll(tempDir)
|
||
|
||
// 创建DEBIAN目录
|
||
debianDir := filepath.Join(tempDir, "DEBIAN")
|
||
err = os.MkdirAll(debianDir, 0755)
|
||
require.NoError(t, err)
|
||
|
||
// 创建测试配置
|
||
cfg := &config.Config{
|
||
Name: "TestApp",
|
||
Version: "1.0.0",
|
||
Description: "Test Application",
|
||
Author: "Test Author",
|
||
Platforms: config.PlatformsConfig{
|
||
Linux: config.LinuxConfig{
|
||
Deb: config.LinuxDebConfig{
|
||
Section: "utils",
|
||
Priority: "optional",
|
||
Architecture: "amd64",
|
||
Depends: []string{"libc6", "libstdc++6"},
|
||
Recommends: []string{"curl"},
|
||
Suggests: []string{"wget"},
|
||
Conflicts: []string{"old-package"},
|
||
Replaces: []string{"legacy-package"},
|
||
},
|
||
},
|
||
},
|
||
}
|
||
|
||
// 创建DEB构建器
|
||
builder := NewDEBBuilder(cfg, tempDir)
|
||
|
||
// 测试创建控制文件
|
||
err = builder.createControlFile(tempDir)
|
||
assert.NoError(t, err)
|
||
|
||
// 验证控制文件是否存在
|
||
controlFile := filepath.Join(tempDir, "DEBIAN", "control")
|
||
_, err = os.Stat(controlFile)
|
||
assert.NoError(t, err)
|
||
|
||
// 读取控制文件内容
|
||
content, err := os.ReadFile(controlFile)
|
||
assert.NoError(t, err)
|
||
|
||
// 验证文件内容
|
||
contentStr := string(content)
|
||
assert.Contains(t, contentStr, "Package: TestApp")
|
||
assert.Contains(t, contentStr, "Version: 1.0.0")
|
||
assert.Contains(t, contentStr, "Section: utils")
|
||
assert.Contains(t, contentStr, "Priority: optional")
|
||
assert.Contains(t, contentStr, "Architecture: amd64")
|
||
assert.Contains(t, contentStr, "Maintainer: Test Author")
|
||
assert.Contains(t, contentStr, "Description: Test Application")
|
||
assert.Contains(t, contentStr, "Depends: libc6, libstdc++6")
|
||
assert.Contains(t, contentStr, "Recommends: curl")
|
||
assert.Contains(t, contentStr, "Suggests: wget")
|
||
assert.Contains(t, contentStr, "Conflicts: old-package")
|
||
assert.Contains(t, contentStr, "Replaces: legacy-package")
|
||
}
|
||
|
||
func TestDEBBuilder_CreateScripts(t *testing.T) {
|
||
// 创建临时测试目录
|
||
tempDir, err := os.MkdirTemp("", "deb-builder-test-*")
|
||
require.NoError(t, err)
|
||
defer os.RemoveAll(tempDir)
|
||
|
||
// 创建DEBIAN目录
|
||
debianDir := filepath.Join(tempDir, "DEBIAN")
|
||
err = os.MkdirAll(debianDir, 0755)
|
||
require.NoError(t, err)
|
||
|
||
// 创建测试脚本源文件
|
||
scriptDir := filepath.Join(tempDir, "scripts")
|
||
err = os.MkdirAll(scriptDir, 0755)
|
||
require.NoError(t, err)
|
||
|
||
preinstScript := filepath.Join(scriptDir, "preinst.sh")
|
||
err = os.WriteFile(preinstScript, []byte("#!/bin/sh\necho 'Pre-install script'"), 0644)
|
||
require.NoError(t, err)
|
||
|
||
// 创建测试配置
|
||
cfg := &config.Config{
|
||
Name: "TestApp",
|
||
Version: "1.0.0",
|
||
Contents: config.ContentsConfig{
|
||
Scripts: []config.ScriptConfig{
|
||
{
|
||
Type: "preinstall",
|
||
Content: "#!/bin/sh\necho 'Pre-install script'",
|
||
},
|
||
{
|
||
Type: "postinstall",
|
||
Source: preinstScript,
|
||
},
|
||
{
|
||
Type: "preuninstall",
|
||
Content: "echo 'Pre-uninstall script'", // 没有shebang
|
||
},
|
||
{
|
||
Type: "invalid",
|
||
Content: "echo 'Invalid script'",
|
||
},
|
||
},
|
||
},
|
||
}
|
||
|
||
// 创建DEB构建器
|
||
builder := NewDEBBuilder(cfg, tempDir)
|
||
|
||
// 测试创建脚本文件
|
||
err = builder.createScripts(tempDir)
|
||
assert.NoError(t, err)
|
||
|
||
// 验证脚本文件是否存在
|
||
preinstFile := filepath.Join(tempDir, "DEBIAN", "preinst")
|
||
_, err = os.Stat(preinstFile)
|
||
assert.NoError(t, err)
|
||
|
||
postinstFile := filepath.Join(tempDir, "DEBIAN", "postinst")
|
||
_, err = os.Stat(postinstFile)
|
||
assert.NoError(t, err)
|
||
|
||
prermFile := filepath.Join(tempDir, "DEBIAN", "prerm")
|
||
_, err = os.Stat(prermFile)
|
||
assert.NoError(t, err)
|
||
|
||
// 读取脚本文件内容
|
||
content, err := os.ReadFile(preinstFile)
|
||
assert.NoError(t, err)
|
||
assert.Contains(t, string(content), "#!/bin/sh")
|
||
assert.Contains(t, string(content), "echo 'Pre-install script'")
|
||
|
||
content, err = os.ReadFile(prermFile)
|
||
assert.NoError(t, err)
|
||
assert.Contains(t, string(content), "#!/bin/sh")
|
||
assert.Contains(t, string(content), "echo 'Pre-uninstall script'")
|
||
}
|
||
|
||
func TestDEBBuilder_Build(t *testing.T) {
|
||
// 跳过实际构建测试,因为它需要dpkg-deb工具
|
||
t.Skip("跳过实际构建测试,因为它需要dpkg-deb工具")
|
||
|
||
// 创建临时测试目录
|
||
tempDir, err := os.MkdirTemp("", "deb-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",
|
||
Platforms: config.PlatformsConfig{
|
||
Linux: config.LinuxConfig{
|
||
Deb: config.LinuxDebConfig{
|
||
Section: "utils",
|
||
Priority: "optional",
|
||
Architecture: "amd64",
|
||
},
|
||
},
|
||
},
|
||
}
|
||
|
||
// 创建DEB构建器
|
||
builder := NewDEBBuilder(cfg, outputDir)
|
||
|
||
// 测试构建
|
||
err = builder.Build()
|
||
assert.NoError(t, err)
|
||
|
||
// 验证输出文件是否存在
|
||
outputFile := filepath.Join(outputDir, "TestApp_1.0.0_amd64.deb")
|
||
_, err = os.Stat(outputFile)
|
||
assert.NoError(t, err)
|
||
}
|
||
|
||
func TestDEBBuilder_BuildDeb(t *testing.T) {
|
||
// 跳过实际构建测试,因为它需要dpkg-deb工具
|
||
t.Skip("跳过实际构建测试,因为它需要dpkg-deb工具")
|
||
|
||
// 创建临时测试目录
|
||
tempDir, err := os.MkdirTemp("", "deb-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",
|
||
}
|
||
|
||
// 创建DEB构建器
|
||
builder := NewDEBBuilder(cfg, outputDir)
|
||
|
||
// 创建DEB包结构
|
||
err = builder.createDebStructure(tempDir)
|
||
assert.NoError(t, err)
|
||
|
||
// 创建控制文件
|
||
err = builder.createControlFile(tempDir)
|
||
assert.NoError(t, err)
|
||
|
||
// 测试构建DEB包
|
||
outputFile := filepath.Join(outputDir, "TestApp_1.0.0_amd64.deb")
|
||
err = builder.buildDeb(tempDir, outputFile)
|
||
assert.NoError(t, err)
|
||
|
||
// 验证输出文件是否存在
|
||
_, err = os.Stat(outputFile)
|
||
assert.NoError(t, err)
|
||
}
|