Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Ensure patterns written to the filesystem in unit tests don't persist #185

Merged
merged 58 commits into from
Jun 1, 2023

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented May 23, 2023

Summary of changes

At the end of each unit test, removes patterns written to the filesystem.

A follow-up to #183 (comment)

How do you know this PR does anything? These unit tests passed before this PR.

Fastest way:

  1. git checkout add/setup-teardown
  2. npm run test:phpunit
  3. Delete all save_pattern_to_file() calls in ModelTest.php
  4. npm run test:phpunit
  5. Expected and actual: there are 2 failures
  6. Before this PR, the tests still passed, even if you deleted the save_pattern_to_file() calls (the written patterns persisted after the tests)

Slowest way:

  1. git checkout 23e03b4fd3df77a5db029b6a98119b553c5c88a6
  2. That gets these tests before this PR
  3. npm run test:phpunit
  4. Go to http://localhost:1001/wp-admin/post-new.php
  5. In the inserter, see the patterns that unit tests created (they persisted after the tests):
Screenshot 2023-05-23 at 5 45 07 PM
  1. Go to http://localhost:1001/wp-admin/themes.php
  2. Delete Twenty Twentythree, reinstall it, and activate it (to delete those patterns)
  3. git checkout add/setup-teardown
  4. npm run test:phpunit
  5. Wait for tests to pass
  6. Go back to http://localhost:1001/wp-admin/post-new.php
  7. Click the inserter
  8. Expected and actual: There's no 'Uncategorized' pattern (patterns written in unit tests didn't persist after the tests):
Screenshot 2023-05-23 at 5 49 40 PM

@kienstra kienstra changed the base branch from main to add/slug-prefixing May 23, 2023 23:38
parent::setUp();
$this->stylesheet_dir = get_wp_filesystem_api()->wp_themes_dir() . '/pm-testing';
get_wp_filesystem_api()->mkdir( $this->stylesheet_dir );
add_filter( 'stylesheet_directory', [ $this, 'get_stylesheet_dir' ] );
Copy link
Contributor Author

@kienstra kienstra May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this example, this makes get_patterns_directory() get the mock theme directory we created for these unit tests:$this->stylesheet_dir.

@kienstra kienstra marked this pull request as ready for review May 24, 2023 00:28
* Add a naive function to update a slug

* Handle multiple attributes in pattern block

* Only output the name and textdomain if they exist
Base automatically changed from add/slug-prefixing to update/patternception-saving-bug May 24, 2023 16:57
'post_name' => 'new-pattern-originally-created-with-pattern-manager',
'post_type' => get_pattern_post_type(),
]
);
Copy link
Contributor Author

@kienstra kienstra May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this if you'd like. It's a tangent.

Copy link
Contributor

@johnstonphilip johnstonphilip Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it still works it's fine with me to keep :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks!

Base automatically changed from update/patternception-saving-bug to main May 24, 2023 19:50
@kienstra
Copy link
Contributor Author

kienstra commented Jun 1, 2023

Thanks, @johnstonphilip!

@kienstra kienstra merged commit ed56885 into main Jun 1, 2023
@kienstra kienstra deleted the add/setup-teardown branch June 1, 2023 18:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants