mirror of
https://github.com/nvms/esr.git
synced 2025-12-16 06:40:52 +00:00
133 lines
3.3 KiB
Go
133 lines
3.3 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func setupTestConfigFile(content string) (string, error) {
|
|
// Create a temporary file
|
|
dir, err := os.MkdirTemp("", "config_test")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
filePath := filepath.Join(dir, "test_config.yml")
|
|
// Write content to the temp file
|
|
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return filePath, nil
|
|
}
|
|
|
|
func TestReadConfigWithDefaultValues(t *testing.T) {
|
|
ymlContent := `bundle: true
|
|
platform: browser
|
|
format: esm
|
|
sourcemap: true
|
|
outdir: public
|
|
html: index.html
|
|
port: 8080
|
|
`
|
|
|
|
filePath, err := setupTestConfigFile(ymlContent)
|
|
if err != nil {
|
|
t.Fatalf("Failed to setup test config file: %v", err)
|
|
}
|
|
defer os.RemoveAll(filepath.Dir(filePath))
|
|
|
|
config, err := ReadConfig(filePath)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read config: %v", err)
|
|
}
|
|
|
|
expected := &Config{
|
|
Common: Common{
|
|
Bundle: true,
|
|
Sourcemap: true,
|
|
Platform: "browser",
|
|
Format: "esm",
|
|
Outdir: "public",
|
|
},
|
|
}
|
|
|
|
if config.Platform != expected.Platform {
|
|
t.Errorf("ReadConfig().SomeField = %+v, expected %+v", config.Platform, expected.Platform)
|
|
}
|
|
|
|
if config.Format != expected.Format {
|
|
t.Errorf("ReadConfig().SomeField = %+v, expected %+v", config.Format, expected.Format)
|
|
}
|
|
|
|
if config.Outdir != expected.Outdir {
|
|
t.Errorf("ReadConfig().SomeField = %+v, expected %+v", config.Outdir, expected.Outdir)
|
|
}
|
|
|
|
if config.Bundle != expected.Bundle {
|
|
t.Errorf("ReadConfig().SomeField = %+v, expected %+v", config.Bundle, expected.Bundle)
|
|
}
|
|
|
|
if config.Sourcemap != expected.Sourcemap {
|
|
t.Errorf("ReadConfig().SomeField = %+v, expected %+v", config.Sourcemap, expected.Sourcemap)
|
|
}
|
|
|
|
if config.Serve.Html != "index.html" {
|
|
t.Errorf("ReadConfig().SomeField = %+v, expected %+v", config.Serve.Html, "index.html")
|
|
}
|
|
|
|
if config.Serve.Port != 8080 {
|
|
t.Errorf("ReadConfig().SomeField = %+v, expected %+v", config.Serve.Port, 8080)
|
|
}
|
|
}
|
|
|
|
func TestReadConfigWithCustomBuildConfig(t *testing.T) {
|
|
ymlContent := `build:
|
|
minify: true
|
|
minifyWhitespace: true
|
|
sourcemap: false
|
|
`
|
|
|
|
filePath, err := setupTestConfigFile(ymlContent)
|
|
if err != nil {
|
|
t.Fatalf("Failed to setup test config file: %v", err)
|
|
}
|
|
defer os.RemoveAll(filepath.Dir(filePath))
|
|
|
|
config, err := ReadConfig(filePath)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read config: %v", err)
|
|
}
|
|
|
|
expected := &BuildConfig{
|
|
Common: Common{
|
|
Minify: true,
|
|
MinifyWhitespace: true,
|
|
MinifyIdentifiers: false,
|
|
Bundle: true,
|
|
Sourcemap: false,
|
|
},
|
|
}
|
|
|
|
if config.Build.Minify != expected.Minify {
|
|
t.Errorf("ReadConfig().SomeField = %+v, expected %+v", config.Build.Minify, expected.Minify)
|
|
}
|
|
|
|
if config.Build.MinifyWhitespace != expected.MinifyWhitespace {
|
|
t.Errorf("ReadConfig().SomeField = %+v, expected %+v", config.Build.MinifyWhitespace, expected.MinifyWhitespace)
|
|
}
|
|
|
|
if config.Build.MinifyIdentifiers != expected.MinifyIdentifiers {
|
|
t.Errorf("ReadConfig().SomeField = %+v, expected %+v", config.Build.MinifyIdentifiers, expected.MinifyIdentifiers)
|
|
}
|
|
|
|
if config.Build.Bundle != expected.Bundle {
|
|
t.Errorf("ReadConfig().SomeField = %+v, expected %+v", config.Build.Bundle, expected.Bundle)
|
|
}
|
|
|
|
if config.Build.Sourcemap != expected.Sourcemap {
|
|
t.Errorf("ReadConfig().SomeField = %+v, expected %+v", config.Build.Sourcemap, expected.Sourcemap)
|
|
}
|
|
}
|