1package main 2 3import ( 4 "fmt" 5 "io/ioutil" 6 "os/exec" 7 "strings" 8 "testing" 9) 10 11func TestMetadata(t *testing.T) { 12 cmd := exec.Command( 13 "metadata", "-rule", "test_spec", "-inputFile", "./inputFiles.txt", "-outputFile", 14 "./generatedOutputFile.txt", 15 ) 16 stderr, err := cmd.CombinedOutput() 17 if err != nil { 18 t.Fatalf("Error running metadata command: %s. Error: %v", stderr, err) 19 } 20 21 // Read the contents of the expected output file 22 expectedOutput, err := ioutil.ReadFile("./expectedOutputFile.txt") 23 if err != nil { 24 t.Fatalf("Error reading expected output file: %s", err) 25 } 26 27 // Read the contents of the generated output file 28 generatedOutput, err := ioutil.ReadFile("./generatedOutputFile.txt") 29 if err != nil { 30 t.Fatalf("Error reading generated output file: %s", err) 31 } 32 33 fmt.Println() 34 35 // Compare the contents 36 if string(expectedOutput) != string(generatedOutput) { 37 t.Errorf("Generated file contents do not match the expected output") 38 } 39} 40 41func TestMetadataNegativeCase(t *testing.T) { 42 cmd := exec.Command( 43 "metadata", "-rule", "test_spec", "-inputFile", "./inputFilesNegativeCase.txt", "-outputFile", 44 "./generatedOutputFileNegativeCase.txt", 45 ) 46 stderr, err := cmd.CombinedOutput() 47 if err == nil { 48 t.Fatalf( 49 "Expected an error, but the metadata command executed successfully. Output: %s", 50 stderr, 51 ) 52 } 53 54 expectedError := "Conflicting trendy team IDs found for java-test-module" + 55 "-name-one at:\nAndroid.bp with teamId: 12346," + 56 "\nAndroid.bp with teamId: 12345" 57 if !strings.Contains( 58 strings.TrimSpace(string(stderr)), strings.TrimSpace(expectedError), 59 ) { 60 t.Errorf( 61 "Unexpected error message. Expected to contain: %s, Got: %s", 62 expectedError, stderr, 63 ) 64 } 65} 66 67func TestEmptyInputFile(t *testing.T) { 68 cmd := exec.Command( 69 "metadata", "-rule", "test_spec", "-inputFile", "./emptyInputFile.txt", "-outputFile", 70 "./generatedEmptyOutputFile.txt", 71 ) 72 stderr, err := cmd.CombinedOutput() 73 if err != nil { 74 t.Fatalf("Error running metadata command: %s. Error: %v", stderr, err) 75 } 76 77 // Read the contents of the generated output file 78 generatedOutput, err := ioutil.ReadFile("./generatedEmptyOutputFile.txt") 79 if err != nil { 80 t.Fatalf("Error reading generated output file: %s", err) 81 } 82 83 fmt.Println() 84 85 // Compare the contents 86 if string(generatedOutput) != "\n" { 87 t.Errorf("Generated file contents do not match the expected output") 88 } 89} 90 91func TestCodeMetadata(t *testing.T) { 92 cmd := exec.Command( 93 "metadata", "-rule", "code_metadata", "-inputFile", "./inputCodeMetadata.txt", "-outputFile", 94 "./generatedCodeMetadataOutputFile.txt", 95 ) 96 stderr, err := cmd.CombinedOutput() 97 if err != nil { 98 t.Fatalf("Error running metadata command: %s. Error: %v", stderr, err) 99 } 100 101 // Read the contents of the expected output file 102 expectedOutput, err := ioutil.ReadFile("./expectedCodeMetadataOutput.txt") 103 if err != nil { 104 t.Fatalf("Error reading expected output file: %s", err) 105 } 106 107 // Read the contents of the generated output file 108 generatedOutput, err := ioutil.ReadFile("./generatedCodeMetadataOutputFile.txt") 109 if err != nil { 110 t.Fatalf("Error reading generated output file: %s", err) 111 } 112 113 fmt.Println() 114 115 // Compare the contents 116 if string(expectedOutput) != string(generatedOutput) { 117 t.Errorf("Generated file contents do not match the expected output") 118 } 119} 120