package processor
import (
"os"
"path/filepath"
"testing"
"cook/utils"
"github.com/stretchr/testify/assert"
)
func TestProcessRegexWithExternalLuaFile(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "lua-external-integration-test-*")
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
// Create a test Lua file with replacement variable
luaFile := filepath.Join(tmpDir, "multiply.lua")
luaContent := `v1 = v1 * 2
replacement = format("%s", v1)
return true`
err = os.WriteFile(luaFile, []byte(luaContent), 0644)
assert.NoError(t, err)
// Create test content
content := `10`
// Create command with external Lua reference
command := utils.ModifyCommand{
Name: "test",
Regex: `(\d+)`,
Lua: "@" + filepath.Base(luaFile),
SourceDir: tmpDir,
}
// Process
modifications, err := ProcessRegex(content, command, "test.xml")
assert.NoError(t, err)
assert.Greater(t, len(modifications), 0)
// Apply modifications
result := content
for _, mod := range modifications {
result = result[:mod.From] + mod.With + result[mod.To:]
}
assert.Contains(t, result, "20")
}
func TestProcessJSONWithExternalLuaFile(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "lua-external-json-integration-test-*")
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
// Create a test Lua file
luaFile := filepath.Join(tmpDir, "json_modify.lua")
luaContent := `data.value = 84
modified = true`
err = os.WriteFile(luaFile, []byte(luaContent), 0644)
assert.NoError(t, err)
// Create test JSON content
content := `{"value": 42}`
// Create command with external Lua reference
command := utils.ModifyCommand{
Name: "test",
JSON: true,
Lua: "@" + filepath.Base(luaFile),
SourceDir: tmpDir,
}
// Process
modifications, err := ProcessJSON(content, command, "test.json")
assert.NoError(t, err)
assert.Greater(t, len(modifications), 0)
// Apply modifications to verify
result := content
for _, mod := range modifications {
result = result[:mod.From] + mod.With + result[mod.To:]
}
// Check that value was changed to 84 (formatting may vary)
assert.Contains(t, result, `"value"`)
assert.Contains(t, result, `84`)
assert.NotContains(t, result, `"value": 42`)
assert.NotContains(t, result, `"value":42`)
}
func TestProcessXMLWithExternalLuaFile(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "lua-external-xml-integration-test-*")
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
// Create a test Lua file (XML uses 'root' not 'data')
luaFile := filepath.Join(tmpDir, "xml_modify.lua")
luaContent := `visitElements(root, function(elem)
if elem._tag == "Item" then
modifyNumAttr(elem, "Weight", function(val) return val * 2 end)
end
end)
modified = true`
err = os.WriteFile(luaFile, []byte(luaContent), 0644)
assert.NoError(t, err)
// Create test XML content
content := ` `
// Create command with external Lua reference
command := utils.ModifyCommand{
Name: "test",
Lua: "@" + filepath.Base(luaFile),
SourceDir: tmpDir,
}
// Process
modifications, err := ProcessXML(content, command, "test.xml")
assert.NoError(t, err)
assert.Greater(t, len(modifications), 0)
// Apply modifications to verify
result := content
for _, mod := range modifications {
result = result[:mod.From] + mod.With + result[mod.To:]
}
assert.Contains(t, result, `Weight="20"`)
}
func TestExternalLuaFileWithVariables(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "lua-external-vars-integration-test-*")
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
// Create a test Lua file with variable reference
luaFile := filepath.Join(tmpDir, "with_vars.lua")
luaContent := `v1 = v1 * $multiply
replacement = format("%s", v1)
return true`
err = os.WriteFile(luaFile, []byte(luaContent), 0644)
assert.NoError(t, err)
// Set global variable
SetVariables(map[string]interface{}{"multiply": 1.5})
defer SetVariables(map[string]interface{}{})
// Create test content
content := `10`
// Create command with external Lua reference
command := utils.ModifyCommand{
Name: "test",
Regex: `(\d+)`,
Lua: "@" + filepath.Base(luaFile),
SourceDir: tmpDir,
}
// Process
modifications, err := ProcessRegex(content, command, "test.xml")
assert.NoError(t, err)
assert.Greater(t, len(modifications), 0)
// Apply modifications
result := content
for _, mod := range modifications {
result = result[:mod.From] + mod.With + result[mod.To:]
}
assert.Contains(t, result, "15")
}
func TestExternalLuaFileErrorHandling(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "lua-external-error-test-*")
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)
// Create command with non-existent external Lua file
command := utils.ModifyCommand{
Name: "test",
Regex: `(\d+)`,
Lua: "@nonexistent.lua",
SourceDir: tmpDir,
}
// Process - the error script will be generated but execution will fail
// ProcessRegex continues on Lua errors, so no modifications will be made
content := `10`
modifications, err := ProcessRegex(content, command, "test.xml")
// No error returned (ProcessRegex continues on Lua errors), but no modifications made
assert.NoError(t, err)
assert.Empty(t, modifications)
}