mirror of
https://gitlab.com/TECHNOFAB/nixtest.git
synced 2026-02-02 11:25:10 +01:00
refactor: split into packages and add tests
This commit is contained in:
parent
fd58344ca7
commit
11117e0c0e
28 changed files with 2736 additions and 636 deletions
54
internal/util/util.go
Normal file
54
internal/util/util.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/akedrou/textdiff"
|
||||
"github.com/akedrou/textdiff/myers"
|
||||
apperrors "gitlab.com/technofab/nixtest/internal/errors"
|
||||
)
|
||||
|
||||
func ComputeDiff(expected, actual string) (string, error) {
|
||||
// FIXME: ComputeEdits deprecated
|
||||
edits := myers.ComputeEdits(expected, actual)
|
||||
diff, err := textdiff.ToUnified("expected", "actual", expected, edits, 3)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// remove newline hint
|
||||
diff = strings.ReplaceAll(diff, "\\ No newline at end of file\n", "")
|
||||
return diff, nil
|
||||
}
|
||||
|
||||
// ParseFile reads and decodes a JSON file into the provided type
|
||||
func ParseFile[T any](filePath string) (result T, err error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return result, &apperrors.FileReadError{Path: filePath, Err: fmt.Errorf("failed to open: %w", err)}
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
decoder := json.NewDecoder(file)
|
||||
err = decoder.Decode(&result)
|
||||
if err != nil {
|
||||
return result, &apperrors.JSONUnmarshalError{Source: filePath, Err: fmt.Errorf("failed to decode: %w", err)}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// PrefixLines adds a prefix to each line of the input string
|
||||
func PrefixLines(input string, prefix string) string {
|
||||
lines := strings.Split(input, "\n")
|
||||
for i := range lines {
|
||||
lines[i] = prefix + lines[i]
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func IsString(value any) bool {
|
||||
_, ok := value.(string)
|
||||
return ok
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue