Add tests
This commit is contained in:
51
main.go
51
main.go
@@ -111,26 +111,29 @@ end
|
||||
|
||||
// Process each file
|
||||
for _, file := range files {
|
||||
err := processFile(file, pattern, fullScript)
|
||||
log.Printf("Processing file: %s", file)
|
||||
fullPath := filepath.Join(".", file)
|
||||
|
||||
content, err := os.ReadFile(fullPath)
|
||||
if err != nil {
|
||||
Error.Printf("error reading file: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
fileContent := string(content)
|
||||
result, err := process(fileContent, pattern, fullScript)
|
||||
if err != nil {
|
||||
Error.Printf("Error processing file %s: %v", file, err)
|
||||
}
|
||||
|
||||
err = os.WriteFile(fullPath, []byte(result), 0644)
|
||||
if err != nil {
|
||||
Error.Printf("error writing file: %v", 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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading file: %v", err)
|
||||
}
|
||||
|
||||
fileContent := string(content)
|
||||
modified := false
|
||||
|
||||
// Initialize Lua state once per file
|
||||
func process(data string, pattern *regexp.Regexp, luaScript string) (string, error) {
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
@@ -138,16 +141,16 @@ func processFile(filename string, pattern *regexp.Regexp, luaScript string) erro
|
||||
L.Push(L.GetGlobal("require"))
|
||||
L.Push(lua.LString("math"))
|
||||
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
|
||||
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
|
||||
result := pattern.ReplaceAllStringFunc(fileContent, func(match string) string {
|
||||
result := pattern.ReplaceAllStringFunc(data, func(match string) string {
|
||||
captures := pattern.FindStringSubmatch(match)
|
||||
if len(captures) <= 1 {
|
||||
// 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
|
||||
result = strings.Replace(result, oldVal, newVal, 1)
|
||||
}
|
||||
modified = true
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -208,16 +210,5 @@ func processFile(filename string, pattern *regexp.Regexp, luaScript string) erro
|
||||
return match
|
||||
})
|
||||
|
||||
// Only write if changes were made
|
||||
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
|
||||
return result, 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