Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate test files where all tests are identical #6690

Merged
merged 1 commit into from
May 7, 2024

Conversation

kbx81
Copy link
Member

@kbx81 kbx81 commented May 7, 2024

What does this implement/fix?

Consolidate test files where tests are identical for all SoCs within a given component/platform.

(Done with a script...I think it worked...)

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Related issue or feature (if applicable): fixes

Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Example entry for config.yaml:

# Example config.yaml

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

@codecov-commenter
Copy link

codecov-commenter commented May 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 54.15%. Comparing base (4d8b5ed) to head (7a0fe49).
Report is 521 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #6690      +/-   ##
==========================================
+ Coverage   53.70%   54.15%   +0.44%     
==========================================
  Files          50       50              
  Lines        9408     9574     +166     
  Branches     1654     1687      +33     
==========================================
+ Hits         5053     5185     +132     
- Misses       4056     4065       +9     
- Partials      299      324      +25     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jesserockz jesserockz merged commit f6a3784 into esphome:dev May 7, 2024
72 checks passed
@kbx81
Copy link
Member Author

kbx81 commented May 7, 2024

Script that did this: test-consolidator.txt

#!/usr/bin/env python3
import filecmp, os, shutil

dry_run = False
path = "tests/components"
# file_starts_with = "atc"
file_prefix = "test."
file_suffix = ".yaml"
common_file_name = "common.yaml"
include_common_yaml = "<<: !include common.yaml\n"
mismatch_count = 0
modification_count = 0
skipped_count = 0

for some_file in os.listdir(path):
    some_test_file_dir = path + "/" + some_file
    if os.path.isdir(some_test_file_dir):  # and some_file.startswith(file_starts_with):
        print(f"*** Directory: {some_test_file_dir}")
        test_file_list = []
        for some_test_file in os.listdir(some_test_file_dir):
            if (
                some_test_file == common_file_name
                or some_test_file.startswith(file_prefix)
                and some_test_file.endswith(file_suffix)
            ):
                test_file_list.append(some_test_file)

        if len(test_file_list):
            print(f"    Test filename(s): {test_file_list}")
        all_files_match = True

        if common_file_name in test_file_list:
            skipped_count += 1
            print(f"    ⛔️ {common_file_name} already exists, skipping...")
        else:
            for test_file in test_file_list:
                if not filecmp.cmp(
                    some_test_file_dir + "/" + test_file,
                    some_test_file_dir + "/" + test_file_list[0],
                ):
                    print(f"    ❌ {test_file} does not match {test_file_list[0]}")
                    mismatch_count += 1
                    all_files_match = False
                    break

            if len(test_file_list):
                if all_files_match:
                    modification_count += 1
                    if dry_run:
                        print(
                            "    ✅ All files matched! Dry run enabled, no files created/changed!"
                        )
                    else:
                        print("    ✅ All files matched! Creating 'common.yaml'...")
                        shutil.copy(
                            some_test_file_dir + "/" + test_file_list[0],
                            some_test_file_dir + "/" + common_file_name,
                        )
                        for test_file in test_file_list:
                            test_file_yaml = open(
                                some_test_file_dir + "/" + test_file, "w"
                            )
                            test_file_yaml.write(include_common_yaml)
                            test_file_yaml.close()

            else:
                print("    😱 No test files found!")

if dry_run:
    print(f"⭐️ Dry run! No tests modified!")
    print(f"    Would modify: {modification_count}")
    print(f"    File(s) mismatch: {mismatch_count}")
    print(f"    Skipped: {skipped_count}")
else:
    print(f"Tests modified: {modification_count}")
    print(f"File(s) mismatch: {mismatch_count}")
    print(f"Skipped: {skipped_count}")

@jesserockz jesserockz mentioned this pull request May 8, 2024
@github-actions github-actions bot locked and limited conversation to collaborators May 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants