Add conversion tests
This commit is contained in:
86
toml_test.go
86
toml_test.go
@@ -423,3 +423,89 @@ files = ["test.txt"
|
||||
assert.Error(t, err, "Should return error for empty TOML file")
|
||||
assert.Nil(t, commands, "Should return nil commands for empty TOML")
|
||||
}
|
||||
|
||||
func TestYAMLToTOMLConversion(t *testing.T) {
|
||||
// Create a temporary directory for testing
|
||||
tmpDir, err := os.MkdirTemp("", "yaml-to-toml-conversion-test")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
// Change to temp directory
|
||||
origDir, _ := os.Getwd()
|
||||
defer os.Chdir(origDir)
|
||||
os.Chdir(tmpDir)
|
||||
|
||||
// Create a test YAML file
|
||||
yamlContent := `- name: "ConversionTest"
|
||||
regex: "value = !num"
|
||||
lua: "v1 * 3"
|
||||
files: ["test.txt"]
|
||||
loglevel: DEBUG
|
||||
|
||||
- name: "AnotherTest"
|
||||
regex: "enabled = (true|false)"
|
||||
lua: "= false"
|
||||
files: ["*.conf"]
|
||||
|
||||
- name: "GlobalModifiers"
|
||||
modifiers:
|
||||
multiplier: 2.5
|
||||
prefix: "CONV_"
|
||||
`
|
||||
|
||||
yamlFile := filepath.Join(tmpDir, "test.yml")
|
||||
err = os.WriteFile(yamlFile, []byte(yamlContent), 0644)
|
||||
assert.NoError(t, err, "Should write YAML test file")
|
||||
|
||||
// Test conversion
|
||||
err = utils.ConvertYAMLToTOML("test.yml")
|
||||
assert.NoError(t, err, "Should convert YAML to TOML without error")
|
||||
|
||||
// Check that TOML file was created
|
||||
tomlFile := filepath.Join(tmpDir, "test.toml")
|
||||
_, err = os.Stat(tomlFile)
|
||||
assert.NoError(t, err, "TOML file should exist after conversion")
|
||||
|
||||
// Read and verify TOML content
|
||||
tomlData, err := os.ReadFile(tomlFile)
|
||||
assert.NoError(t, err, "Should read TOML file")
|
||||
|
||||
tomlContent := string(tomlData)
|
||||
assert.Contains(t, tomlContent, `name = "ConversionTest"`, "TOML should contain first command name")
|
||||
assert.Contains(t, tomlContent, `name = "AnotherTest"`, "TOML should contain second command name")
|
||||
assert.Contains(t, tomlContent, `name = "GlobalModifiers"`, "TOML should contain global modifiers command")
|
||||
assert.Contains(t, tomlContent, `multiplier = 2.5`, "TOML should contain multiplier")
|
||||
assert.Contains(t, tomlContent, `prefix = "CONV_"`, "TOML should contain prefix")
|
||||
|
||||
// Test that converted TOML loads correctly
|
||||
commands, err := utils.LoadCommandsFromTomlFiles("test.toml")
|
||||
assert.NoError(t, err, "Should load converted TOML without error")
|
||||
assert.Len(t, commands, 3, "Should load 3 commands from converted TOML")
|
||||
|
||||
// Find global modifiers command (it might not be first)
|
||||
var globalCmd utils.ModifyCommand
|
||||
foundGlobal := false
|
||||
for _, cmd := range commands {
|
||||
if cmd.Name == "GlobalModifiers" {
|
||||
globalCmd = cmd
|
||||
foundGlobal = true
|
||||
break
|
||||
}
|
||||
}
|
||||
assert.True(t, foundGlobal, "Should find global modifiers command")
|
||||
assert.Equal(t, 2.5, globalCmd.Modifiers["multiplier"], "Should preserve multiplier value")
|
||||
assert.Equal(t, "CONV_", globalCmd.Modifiers["prefix"], "Should preserve prefix value")
|
||||
|
||||
// Test skip functionality - run conversion again
|
||||
err = utils.ConvertYAMLToTOML("test.yml")
|
||||
assert.NoError(t, err, "Should handle existing TOML file without error")
|
||||
|
||||
// Verify original TOML file wasn't modified
|
||||
originalTomlData, err := os.ReadFile(tomlFile)
|
||||
assert.NoError(t, err, "Should read TOML file again")
|
||||
assert.Equal(t, tomlData, originalTomlData, "TOML file content should be unchanged")
|
||||
|
||||
t.Logf("YAML to TOML conversion test completed successfully")
|
||||
}
|
||||
Reference in New Issue
Block a user