94 lines
2.6 KiB
Go
94 lines
2.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// TestConvertYAMLToTOMLReadError tests error handling when YAML file can't be read
|
|
func TestConvertYAMLToTOMLReadError(t *testing.T) {
|
|
tmpDir, err := os.MkdirTemp("", "convert-read-error-*")
|
|
assert.NoError(t, err)
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
// Create YAML file with no read permissions (on Unix) or delete it after creation
|
|
yamlFile := filepath.Join(tmpDir, "test.yml")
|
|
err = os.WriteFile(yamlFile, []byte("commands:\n - name: test\n"), 0000)
|
|
assert.NoError(t, err)
|
|
|
|
origDir, _ := os.Getwd()
|
|
defer os.Chdir(origDir)
|
|
os.Chdir(tmpDir)
|
|
|
|
// This should fail to read but not crash
|
|
err = ConvertYAMLToTOML("test.yml")
|
|
// Function continues on error, doesn't return error
|
|
assert.NoError(t, err)
|
|
|
|
// Fix permissions for cleanup
|
|
os.Chmod(yamlFile, 0644)
|
|
}
|
|
|
|
// TestConvertYAMLToTOMLParseError tests error handling when YAML is invalid
|
|
func TestConvertYAMLToTOMLParseError(t *testing.T) {
|
|
tmpDir, err := os.MkdirTemp("", "convert-parse-error-*")
|
|
assert.NoError(t, err)
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
// Create invalid YAML
|
|
yamlFile := filepath.Join(tmpDir, "invalid.yml")
|
|
err = os.WriteFile(yamlFile, []byte("commands:\n - [this is not valid yaml}}"), 0644)
|
|
assert.NoError(t, err)
|
|
|
|
origDir, _ := os.Getwd()
|
|
defer os.Chdir(origDir)
|
|
os.Chdir(tmpDir)
|
|
|
|
// This should fail to parse but not crash
|
|
err = ConvertYAMLToTOML("invalid.yml")
|
|
assert.NoError(t, err)
|
|
|
|
// TOML file should not exist
|
|
_, statErr := os.Stat(filepath.Join(tmpDir, "invalid.toml"))
|
|
assert.True(t, os.IsNotExist(statErr))
|
|
}
|
|
|
|
// TestConvertYAMLToTOMLWriteError tests error handling when TOML file can't be written
|
|
func TestConvertYAMLToTOMLWriteError(t *testing.T) {
|
|
if os.Getenv("CI") != "" {
|
|
t.Skip("Skipping write permission test in CI")
|
|
}
|
|
|
|
tmpDir, err := os.MkdirTemp("", "convert-write-error-*")
|
|
assert.NoError(t, err)
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
// Create valid YAML
|
|
yamlFile := filepath.Join(tmpDir, "test.yml")
|
|
err = os.WriteFile(yamlFile, []byte("commands:\n - name: test\n regex: test\n lua: v1\n files: [test.txt]\n"), 0644)
|
|
assert.NoError(t, err)
|
|
|
|
// Create output directory with no write permissions
|
|
outputDir := filepath.Join(tmpDir, "readonly")
|
|
err = os.Mkdir(outputDir, 0555)
|
|
assert.NoError(t, err)
|
|
defer os.Chmod(outputDir, 0755) // Fix for cleanup
|
|
|
|
origDir, _ := os.Getwd()
|
|
defer os.Chdir(origDir)
|
|
os.Chdir(tmpDir)
|
|
|
|
// Move YAML into readonly dir
|
|
newYamlFile := filepath.Join(outputDir, "test.yml")
|
|
os.Rename(yamlFile, newYamlFile)
|
|
|
|
os.Chdir(outputDir)
|
|
|
|
// This should fail to write but not crash
|
|
err = ConvertYAMLToTOML("test.yml")
|
|
assert.NoError(t, err)
|
|
}
|