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

Commit 3e3d88e9 authored by Sorin Basca's avatar Sorin Basca
Browse files

Allow alternate golden images for TestWithGoldenOutput

Since build flags could change the output to be checked, it's
good to have the ability to check against different sets of
golden outputs and find one that matches.

This solves the problem of having some builds using
RELEASE_TARGET_JAVA_21 and others not.

Fixes: 378470825
Test: atest --host tiny-framework-dump-test:__main__.TestWithGoldenOutput#test_compare_to_golden
Change-Id: I1b10e4b65673b351fe70a04cb2bf6ba58e0878cd
parent 22113b5f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -156,6 +156,7 @@ python_test_host {
    ],
    data: [
        "golden-output/*.txt",
        "golden-output.RELEASE_TARGET_JAVA_21/*.txt",
    ],
    java_data: [
        "hoststubgen-test-tiny-framework-orig-dump",
+3697 −0

File added.

Preview size limit exceeded, changes collapsed.

+3727 −0

File added.

Preview size limit exceeded, changes collapsed.

+4896 −0

File added.

Preview size limit exceeded, changes collapsed.

+34 −14
Original line number Diff line number Diff line
@@ -20,18 +20,23 @@ import os
import unittest
import subprocess

GOLDEN_DIR = 'golden-output'
GOLDEN_DIRS = [
    'golden-output',
    'golden-output.RELEASE_TARGET_JAVA_21',
]


# Run diff.
def run_diff(file1, file2):
    command = ['diff', '-u', '--ignore-blank-lines', '--ignore-space-change', file1, file2]
    command = ['diff', '-u', '--ignore-blank-lines',
               '--ignore-space-change', file1, file2]
    print(' '.join(command))
    result = subprocess.run(command, stderr=sys.stdout)

    success = result.returncode == 0

    if success:
        print(f'No diff found.')
        print('No diff found.')
    else:
        print(f'Fail: {file1} and {file2} are different.')

@@ -39,27 +44,42 @@ def run_diff(file1, file2):


# Check one golden file.
def check_one_file(filename):
def check_one_file(golden_dir, filename):
    print(f'= Checking file: {filename}')
    return run_diff(os.path.join(GOLDEN_DIR, filename), filename)
    return run_diff(os.path.join(golden_dir, filename), filename)


class TestWithGoldenOutput(unittest.TestCase):

    # Test to check the generated jar files to the golden output.
    # Depending on build flags, the golden output may differ in expected ways.
    # So only expect the files to match one of the possible golden outputs.
    def test_compare_to_golden(self):
        self.skipTest("test cannot handle multiple images (see b/378470825)")
        files = os.listdir(GOLDEN_DIR)
        files.sort()
        success = False

        print(f"Golden files: {files}")
        for golden_dir in GOLDEN_DIRS:
            if self.matches_golden(golden_dir):
                success = True
                print(f"Test passes for dir: {golden_dir}")
                break

        if not success:
            self.fail('Some files are different. ' +
                      'See stdout log for more details.')

    def matches_golden(self, golden_dir):
        files = os.listdir(golden_dir)
        files.sort()

        print(f"Golden files for {golden_dir}: {files}")
        match_success = True

        for file in files:
            if not check_one_file(file):
                success = False
            if not check_one_file(golden_dir, file):
                match_success = False

        return match_success

        if not success:
            self.fail('Some files are different. See stdout log for more details.')

if __name__ == "__main__":
    unittest.main(verbosity=2)