Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6c84079d authored by Aditya Choudhary's avatar Aditya Choudhary Committed by Automerger Merge Worker
Browse files

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

parents a819cb1e d5e2717e
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -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))
+1 −0
Original line number Diff line number Diff line
+1 −0
Original line number Diff line number Diff line
+24 −0
Original line number Diff line number Diff line
@@ -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")
	}
}