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

Commit cf0053ef authored by Aditya Choudhary's avatar Aditya Choudhary
Browse files

Handle empty input file case in Metadata generation

Bug: 296873595
Test: Manual test (use go test inside tools/metadata/testdata)

Ignore-AOSP-First: CPing test_spec rule to udc-mainline-prod to support migration of test targets. Cherry pick of: aosp/2835510

Change-Id: Ib48e88bdad4c870be3ca955abfd64ee8dd74f85d
Merged-In: Ib48e88bdad4c870be3ca955abfd64ee8dd74f85d
parent f101d2b7
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")
	}
}