Add tests
This commit is contained in:
49
main.go
49
main.go
@@ -111,26 +111,29 @@ end
|
|||||||
|
|
||||||
// Process each file
|
// Process each file
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
err := processFile(file, pattern, fullScript)
|
log.Printf("Processing file: %s", file)
|
||||||
if err != nil {
|
fullPath := filepath.Join(".", file)
|
||||||
Error.Printf("Error processing file %s: %v", file, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func processFile(filename string, pattern *regexp.Regexp, luaScript string) error {
|
|
||||||
log.Printf("Processing file: %s", filename)
|
|
||||||
fullPath := filepath.Join(".", filename)
|
|
||||||
|
|
||||||
content, err := os.ReadFile(fullPath)
|
content, err := os.ReadFile(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error reading file: %v", err)
|
Error.Printf("error reading file: %v", err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fileContent := string(content)
|
fileContent := string(content)
|
||||||
modified := false
|
result, err := process(fileContent, pattern, fullScript)
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("Error processing file %s: %v", file, err)
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize Lua state once per file
|
err = os.WriteFile(fullPath, []byte(result), 0644)
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("error writing file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func process(data string, pattern *regexp.Regexp, luaScript string) (string, error) {
|
||||||
L := lua.NewState()
|
L := lua.NewState()
|
||||||
defer L.Close()
|
defer L.Close()
|
||||||
|
|
||||||
@@ -138,16 +141,16 @@ func processFile(filename string, pattern *regexp.Regexp, luaScript string) erro
|
|||||||
L.Push(L.GetGlobal("require"))
|
L.Push(L.GetGlobal("require"))
|
||||||
L.Push(lua.LString("math"))
|
L.Push(lua.LString("math"))
|
||||||
if err := L.PCall(1, 1, nil); err != nil {
|
if err := L.PCall(1, 1, nil); err != nil {
|
||||||
return fmt.Errorf("error loading Lua math library: %v", err)
|
return data, fmt.Errorf("error loading Lua math library: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the Lua script
|
// Load the Lua script
|
||||||
if err := L.DoString(luaScript); err != nil {
|
if err := L.DoString(luaScript); err != nil {
|
||||||
return fmt.Errorf("error in Lua script: %v", err)
|
return data, fmt.Errorf("error in Lua script: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process all regex matches
|
// Process all regex matches
|
||||||
result := pattern.ReplaceAllStringFunc(fileContent, func(match string) string {
|
result := pattern.ReplaceAllStringFunc(data, func(match string) string {
|
||||||
captures := pattern.FindStringSubmatch(match)
|
captures := pattern.FindStringSubmatch(match)
|
||||||
if len(captures) <= 1 {
|
if len(captures) <= 1 {
|
||||||
// No capture groups, return unchanged
|
// No capture groups, return unchanged
|
||||||
@@ -200,7 +203,6 @@ func processFile(filename string, pattern *regexp.Regexp, luaScript string) erro
|
|||||||
// Replace old value with new value
|
// Replace old value with new value
|
||||||
result = strings.Replace(result, oldVal, newVal, 1)
|
result = strings.Replace(result, oldVal, newVal, 1)
|
||||||
}
|
}
|
||||||
modified = true
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,16 +210,5 @@ func processFile(filename string, pattern *regexp.Regexp, luaScript string) erro
|
|||||||
return match
|
return match
|
||||||
})
|
})
|
||||||
|
|
||||||
// Only write if changes were made
|
return result, nil
|
||||||
if modified {
|
|
||||||
err = os.WriteFile(fullPath, []byte(result), 0644)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error writing file: %v", err)
|
|
||||||
}
|
|
||||||
log.Printf("File %s updated successfully", filename)
|
|
||||||
} else {
|
|
||||||
log.Printf("No changes made to %s", filename)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
204
main_test.go
Normal file
204
main_test.go
Normal file
@@ -0,0 +1,204 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSimpleFileSimpleExpression(t *testing.T) {
|
||||||
|
fileContents := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>100</value>
|
||||||
|
</item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
expected := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>150</value>
|
||||||
|
</item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
|
||||||
|
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
||||||
|
luaExpr := `*1.5`
|
||||||
|
|
||||||
|
modifiedContent, err := process(fileContents, regex, luaExpr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if modifiedContent != expected {
|
||||||
|
t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSimpleFileVExpression(t *testing.T) {
|
||||||
|
fileContents := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>100</value>
|
||||||
|
</item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
expected := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>150</value>
|
||||||
|
</item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
|
||||||
|
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
||||||
|
luaExpr := `v1 * 1.5`
|
||||||
|
|
||||||
|
modifiedContent, err := process(fileContents, regex, luaExpr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if modifiedContent != expected {
|
||||||
|
t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSimpleFileTableVExpression(t *testing.T) {
|
||||||
|
fileContents := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>100</value>
|
||||||
|
</item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
expected := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>150</value>
|
||||||
|
</item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
|
||||||
|
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
||||||
|
luaExpr := `v[1] * 1.5`
|
||||||
|
|
||||||
|
modifiedContent, err := process(fileContents, regex, luaExpr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if modifiedContent != expected {
|
||||||
|
t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComplexFileSimpleExpression(t *testing.T) {
|
||||||
|
fileContents := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>100</value>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<value>200</value>
|
||||||
|
</item>
|
||||||
|
<item> <value>300</value> </item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
expected := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>150</value>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<value>300</value>
|
||||||
|
</item>
|
||||||
|
<item> <value>450</value> </item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
|
||||||
|
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
||||||
|
luaExpr := `*1.5`
|
||||||
|
|
||||||
|
modifiedContent, err := process(fileContents, regex, luaExpr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if modifiedContent != expected {
|
||||||
|
t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComplexFileVExpression(t *testing.T) {
|
||||||
|
fileContents := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>100</value>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<value>200</value>
|
||||||
|
</item>
|
||||||
|
<item> <value>300</value> </item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
expected := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>150</value>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<value>300</value>
|
||||||
|
</item>
|
||||||
|
<item> <value>450</value> </item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
|
||||||
|
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
||||||
|
luaExpr := `v1 * 1.5`
|
||||||
|
|
||||||
|
modifiedContent, err := process(fileContents, regex, luaExpr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if modifiedContent != expected {
|
||||||
|
t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComplexFileTableVExpression(t *testing.T) {
|
||||||
|
fileContents := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>100</value>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<value>200</value>
|
||||||
|
</item>
|
||||||
|
<item> <value>300</value> </item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
expected := `
|
||||||
|
<config>
|
||||||
|
<item>
|
||||||
|
<value>150</value>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<value>300</value>
|
||||||
|
</item>
|
||||||
|
<item> <value>450</value> </item>
|
||||||
|
</config>
|
||||||
|
`
|
||||||
|
|
||||||
|
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
||||||
|
luaExpr := `v[1] * 1.5`
|
||||||
|
|
||||||
|
modifiedContent, err := process(fileContents, regex, luaExpr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if modifiedContent != expected {
|
||||||
|
t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user