Merge "Handle empty input file case in Metadata generation" into main

This commit is contained in:
Aditya Choudhary
2023-11-17 12:19:55 +00:00
committed by Gerrit Code Review
4 changed files with 44 additions and 0 deletions

View File

@@ -73,6 +73,20 @@ func readFileToString(filePath string) string {
return string(data)
}
func writeNewlineToOutputFile(outputFile string) {
file, err := os.Create(outputFile)
data := "\n"
if err != nil {
log.Fatal(err)
}
defer file.Close()
_, err = file.Write([]byte(data))
if err != nil {
log.Fatal(err)
}
}
func processTestSpecProtobuf(
filePath string, ownershipMetadataMap *sync.Map, keyLocks *keyToLocksMap,
errCh chan error, wg *sync.WaitGroup,
@@ -140,6 +154,10 @@ func main() {
inputFileData := strings.TrimRight(readFileToString(*inputFile), "\n")
filePaths := strings.Split(inputFileData, "\n")
if len(filePaths) == 1 && filePaths[0] == "" {
writeNewlineToOutputFile(*outputFile)
return
}
ownershipMetadataMap := &sync.Map{}
keyLocks := &keyToLocksMap{}
errCh := make(chan error, len(filePaths))

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@

View File

@@ -63,3 +63,27 @@ func TestMetadataNegativeCase(t *testing.T) {
)
}
}
func TestEmptyInputFile(t *testing.T) {
cmd := exec.Command(
"metadata", "-rule", "test_spec", "-inputFile", "./emptyInputFile.txt", "-outputFile",
"./generatedEmptyOutputFile.txt",
)
stderr, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Error running metadata command: %s. Error: %v", stderr, err)
}
// Read the contents of the generated output file
generatedOutput, err := ioutil.ReadFile("./generatedEmptyOutputFile.txt")
if err != nil {
t.Fatalf("Error reading generated output file: %s", err)
}
fmt.Println()
// Compare the contents
if string(generatedOutput) != "\n" {
t.Errorf("Generated file contents do not match the expected output")
}
}