Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for multiple languages and the build prompt. #186

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
f98d8f2
Merge branch 'plandex-ai:main' into feature/evals
ZanzyTHEbar Jun 26, 2024
33d472e
chore: Add advanced evaluation methods for test cases
ZanzyTHEbar Jun 28, 2024
203e209
chore: Update test cases with new evaluation methods
ZanzyTHEbar Jun 28, 2024
6c34630
chore: migrate tests from verify to yaml file
ZanzyTHEbar Jul 2, 2024
b7c610b
chore: Update test file paths for tests in fix directory
ZanzyTHEbar Jul 2, 2024
67798fd
chore: Update test file paths for tests in fix directory
ZanzyTHEbar Jul 2, 2024
935db3a
chore: setup multi-language tests for build dir
ZanzyTHEbar Jul 2, 2024
f91e53a
chore: begin migrating language tests to individual files
ZanzyTHEbar Jul 10, 2024
aeb4d8b
chore: continue migrating out languages tests to individual files
ZanzyTHEbar Jul 11, 2024
05424f3
chore: js and python tests
ZanzyTHEbar Jul 11, 2024
ee22522
chore: Add error handling to `processData`, update TODO comment, and …
ZanzyTHEbar Jul 11, 2024
a0e053f
chore: Add pdx line nums to tests
ZanzyTHEbar Jul 11, 2024
d0541ce
chore: cleaning up tests in build dir
ZanzyTHEbar Jul 16, 2024
6b0dfe9
chore: update md files for connection and data models
ZanzyTHEbar Jul 16, 2024
899a696
chore: update md files for network and sort function
ZanzyTHEbar Jul 16, 2024
5fbeb6f
chore: update md file user handler
ZanzyTHEbar Jul 16, 2024
5be03ff
chore: update md file user management js test
ZanzyTHEbar Jul 16, 2024
655b928
chore: Add error handling to `processData`, update TODO comment, and …
ZanzyTHEbar Jul 16, 2024
5bdb12b
chore: implement base-line asset files for fix test directory
ZanzyTHEbar Jul 19, 2024
1374559
chore: fix asset generation logic
ZanzyTHEbar Jul 19, 2024
016a87f
chore: minor updates to some of the assets
ZanzyTHEbar Jul 19, 2024
04d34ad
chore: migrate removal go assets to new file format
ZanzyTHEbar Jul 19, 2024
97782f4
chore: add assets for verify tests
ZanzyTHEbar Jul 19, 2024
407e02b
chore: finishing up verify
ZanzyTHEbar Jul 26, 2024
91c88d3
chore: split diffs and changes into separate dirs
ZanzyTHEbar Jul 29, 2024
4aa73a8
chore: finish java code for verify
ZanzyTHEbar Jul 29, 2024
8362643
chore: Refactor Java code and fix TODO comments
ZanzyTHEbar Jul 29, 2024
1c0982a
chore: finish up python code for verify test
ZanzyTHEbar Jul 29, 2024
22e2f10
chore: begin preparring to write tests
ZanzyTHEbar Jul 29, 2024
b0b9c7a
chore: begin preparring to write tests
ZanzyTHEbar Jul 29, 2024
de581c7
chore: finalizing the fix directory
ZanzyTHEbar Aug 7, 2024
24ff033
chore: finish assests for fix test dir
ZanzyTHEbar Aug 7, 2024
654ab7f
chore: Add line nums to fix post files
ZanzyTHEbar Aug 7, 2024
52f5900
chore: Add line nums to fix post files
ZanzyTHEbar Aug 7, 2024
01318c1
chore: Add line nums to fix post files
ZanzyTHEbar Aug 7, 2024
8c97e97
Merge branch 'plandex-ai:main' into feature/evals
ZanzyTHEbar Aug 7, 2024
205a32f
Merge pull request #3 from ZanzyTHEbar/feature/evals
ZanzyTHEbar Aug 7, 2024
568e717
chore: finish tests for the fix directory
ZanzyTHEbar Aug 7, 2024
54c5cfd
chore: implement the go tests for verify directory
ZanzyTHEbar Aug 7, 2024
365d310
chore: update test names to include the language identifier
ZanzyTHEbar Aug 7, 2024
f013921
chore: add all language tests for verify dir
ZanzyTHEbar Aug 7, 2024
74ef66f
chore: rename tests in fix dir to include language id
ZanzyTHEbar Aug 7, 2024
f8af6fc
chore: add js language tests to fix
ZanzyTHEbar Aug 7, 2024
5d913ea
chore: add html and java tests to fix
ZanzyTHEbar Aug 7, 2024
7842c4f
Merge pull request #4 from ZanzyTHEbar/feature/evals
ZanzyTHEbar Aug 13, 2024
eff36ce
chore: write post asset code files for build
ZanzyTHEbar Aug 15, 2024
e788158
chore: setup initial build test structure
ZanzyTHEbar Aug 15, 2024
2f94f01
Merge pull request #5 from ZanzyTHEbar/feature/evals
ZanzyTHEbar Aug 15, 2024
05f2319
chore: build tests complete
ZanzyTHEbar Aug 16, 2024
598e26b
Merge pull request #6 from ZanzyTHEbar/feature/evals
ZanzyTHEbar Aug 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ help:
@echo " make clean - to remove generated files and directories"
@echo " make help - to display this help message"

devTests:
@$(GOCMD) run app/scripts/cmd/dev/dev.go $(filter-out $@,$(MAKECMDGOALS))

# Prevents make from interpreting the arguments as targets
%:
@:
Expand Down
158 changes: 158 additions & 0 deletions app/scripts/cmd/dev/dev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package main

import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)

func main() {
if len(os.Args) != 3 {
fmt.Println("Usage: go run script.go <input_directory> <output_directory>")
os.Exit(1)
}

inputDir := os.Args[1]
outputDir := os.Args[2]

// Check if the input directory exists
if _, err := os.Stat(inputDir); os.IsNotExist(err) {
fmt.Printf("Error: input directory %s does not exist\n", inputDir)
os.Exit(1)
}

// Check if the output directory exists, create if it doesn't
if _, err := os.Stat(outputDir); os.IsNotExist(err) {
if err := os.MkdirAll(outputDir, 0755); err != nil {
fmt.Printf("Error creating output directory %s: %v\n", outputDir, err)
os.Exit(1)
}
}

// Walk through the files in the input directory
err := filepath.Walk(inputDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// Skip directories
if info.IsDir() {
return nil
}

// Check for the corresponding ".post" file
if !strings.Contains(info.Name(), ".post.") {
baseName := strings.TrimSuffix(info.Name(), filepath.Ext(info.Name()))
ext := filepath.Ext(info.Name())
postFile := filepath.Join(inputDir, baseName+".post"+ext)

if _, err := os.Stat(postFile); err == nil {
// Prepare the output file name
outputFileName := fmt.Sprintf("%s.diff.txt", baseName)
outputFilePath := filepath.Join(outputDir, outputFileName)

// Prepare the diff command
cmd := exec.Command("diff", "-u", path, postFile)

// Redirect the output to the output file
outfile, err := os.Create(outputFilePath)
if err != nil {
fmt.Printf("Error creating output file %s: %v\n", outputFilePath, err)
return err
}
defer outfile.Close()

cmd.Stdout = outfile
cmd.Stderr = os.Stderr

// Run the command
err = cmd.Run()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
// Check the exit code
if exitError.ExitCode() == 1 {
// diff found differences
fmt.Printf("Differences found and written to %s\n", outputFilePath)
} else {
fmt.Printf("Error running diff command on %s and %s: %v\n", path, postFile, err)
return err
}
} else {
fmt.Printf("Error running diff command on %s and %s: %v\n", path, postFile, err)
return err
}
} else {
fmt.Printf("No differences found between %s and %s\n", path, postFile)
}

// Now modify the original file
err = modifyFile(path)
if err != nil {
fmt.Printf("Error modifying file %s: %v\n", path, err)
return err
}
}
}

return nil
})

if err != nil {
fmt.Printf("Error walking the path %s: %v\n", inputDir, err)
os.Exit(1)
}
}

func modifyFile(filePath string) error {
inputFile, err := os.Open(filePath)
if err != nil {
return err
}
defer inputFile.Close()

outputFilePath := filePath + ".tmp"
outputFile, err := os.Create(outputFilePath)
if err != nil {
return err
}
defer outputFile.Close()

scanner := bufio.NewScanner(inputFile)
writer := bufio.NewWriter(outputFile)

lineNum := 1
for scanner.Scan() {
line := scanner.Text()
newLine := fmt.Sprintf("pdx-%d: %s\n", lineNum, line)
if _, err := writer.WriteString(newLine); err != nil {
return err
}
lineNum++
}

if err := scanner.Err(); err != nil {
return err
}

if err := writer.Flush(); err != nil {
return err
}

if err := inputFile.Close(); err != nil {
return err
}

if err := outputFile.Close(); err != nil {
return err
}

// Replace the original file with the modified file
if err := os.Rename(outputFilePath, filePath); err != nil {
return err
}

return nil
}
109 changes: 109 additions & 0 deletions app/scripts/cmd/pdx/pdx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package main

import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
)

func main() {
if len(os.Args) < 2 {
fmt.Println("pdx_app <directory>")
return
}

dir := os.Args[1]

// detect if we are in a directory or a file
fileInfo, err := os.Stat(dir)
if err != nil {
fmt.Printf("Error getting file info: %v\n", err)
return
}

if fileInfo.IsDir() {
fmt.Print("Processing directory\n")
err := filepath.Walk(dir, processFile)
if err != nil {
fmt.Printf("Error walking the directory: %v\n", err)
}
} else {
fmt.Print("Processing file\n")
err = processFile(dir, fileInfo, nil)
if err != nil {
fmt.Printf("Error processing file %s: %v\n", dir, err)
}
}

}

func processFile(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// Check if the file matches the pattern <name>.post.<ext>
matched, err := regexp.MatchString(`.*\.post\..*`, info.Name())
if err != nil {
return err
}

if matched {
}
modifyFile(path)

return nil
}

func modifyFile(filePath string) error {
inputFile, err := os.Open(filePath)
if err != nil {
return err
}
defer inputFile.Close()

outputFilePath := filePath + ".tmp"
outputFile, err := os.Create(outputFilePath)
if err != nil {
return err
}
defer outputFile.Close()

scanner := bufio.NewScanner(inputFile)
writer := bufio.NewWriter(outputFile)

lineNum := 1
for scanner.Scan() {
line := scanner.Text()
newLine := fmt.Sprintf("pdx-%d: %s\n", lineNum, line)
if _, err := writer.WriteString(newLine); err != nil {
return err
}
lineNum++
}

if err := scanner.Err(); err != nil {
return err
}

if err := writer.Flush(); err != nil {
return err
}

if err := inputFile.Close(); err != nil {
return err
}

if err := outputFile.Close(); err != nil {
return err
}

// Replace the original file with the modified file
if err := os.Rename(outputFilePath, filePath); err != nil {
return err
}

return nil
}
119 changes: 119 additions & 0 deletions test/evals/promptfoo-poc/build/assets/go/changes/connection.changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
### Subtask 1: Update `init` function to include error handling and retry logic for database connection.

```go
package database

import "database/sql"

var db *sql.DB

func init() {
// Initialize database connection
var err error
for i := 0; i < 3; i++ {
db, err = sql.Open(" postgres", "user=postgres password=postgres dbname=postgres sslmode=disable")
if err != nil {
log.Println("Error connecting to database: ", err)
time.Sleep(5 * time.Second)
continue
}
err = db.Ping()
if err != nil {
log.Println("Error connecting to database: ", err)
time.Sleep(5 * time.Second)
continue
}
break
}
}

func GetConnection() *sql.DB {
return db
}
```

### Subtask 2: Add logging for successful connection or failure in the `init` method.

```go
package database

import (
"database/sql"
"log"
)

var db *sql.DB

func init() {
// Initialize database connection

var err error
for i := 0; i < 3; i++ {
db, err = sql.Open("postgres", "user=postgres password=postgres dbname=postgres sslmode=disable")
if err != nil {
log.Println("Error connecting to database: ", err)
time.Sleep(5 * time.Second)
continue
}
err = db.Ping()
if err != nil {
log.Println("Error connecting to database: ", err)
time.Sleep(5 * time.Second)
continue
}
log.Println("Connected to database")
break
}
}

func GetConnection() *sql.DB {
return db
}
```

### Subtask 3: Provide a new function `CloseConnection` to properly close the database connection.

```go
package database

import (
"database/sql"
"log"
)

var db *sql.DB

func init() {
// Initialize database connection

var err error
for i := 0; i < 3; i++ {
db, err = sql.Open("postgres", "user=postgres password=postgres dbname=postgres sslmode=disable")
if err != nil {
log.Println("Error connecting to database: ", err)
time.Sleep(5 * time.Second)
continue
}
err = db.Ping()
if err != nil {
log.Println("Error connecting to database: ", err)
time.Sleep(5 * time.Second)
continue
}
log.Println("Connected to database")
break
}
}

func GetConnection() *sql.DB {
return db
}


func CloseConnection() {
if db != nil {
db.Close()
log.Println("Database connection closed")
}
}
```
Loading