Merge "Handle empty input file case in Metadata generation" into main am: d5e2717e27
Original change: https://android-review.googlesource.com/c/platform/build/+/2835510 Change-Id: Iafefabdfdbe8557f1ffade6dd1f96f3415e0d9d5 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
@@ -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
tools/metadata/testdata/emptyInputFile.txt
vendored
Normal file
1
tools/metadata/testdata/emptyInputFile.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
|
1
tools/metadata/testdata/generatedEmptyOutputFile.txt
vendored
Normal file
1
tools/metadata/testdata/generatedEmptyOutputFile.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
|
24
tools/metadata/testdata/metadata_test.go
vendored
24
tools/metadata/testdata/metadata_test.go
vendored
@@ -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")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user