Aditya Choudhary | 51f97c1 | 2023-11-02 11:18:40 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io/ioutil" |
| 6 | "os/exec" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestMetadata(t *testing.T) { |
| 12 | cmd := exec.Command( |
Aditya Choudhary | a96ce32 | 2023-11-15 11:02:37 +0000 | [diff] [blame] | 13 | "metadata", "-rule", "test_spec", "-inputFile", "./inputFiles.txt", "-outputFile", |
Aditya Choudhary | 51f97c1 | 2023-11-02 11:18:40 +0000 | [diff] [blame] | 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 | |
| 41 | func TestMetadataNegativeCase(t *testing.T) { |
| 42 | cmd := exec.Command( |
Aditya Choudhary | a96ce32 | 2023-11-15 11:02:37 +0000 | [diff] [blame] | 43 | "metadata", "-rule", "test_spec", "-inputFile", "./inputFilesNegativeCase.txt", "-outputFile", |
Aditya Choudhary | 51f97c1 | 2023-11-02 11:18:40 +0000 | [diff] [blame] | 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 | } |
Aditya Choudhary | 70fb37e | 2023-11-16 19:52:44 +0000 | [diff] [blame^] | 66 | |
| 67 | func 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 | } |