Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@CraigSiemens
Copy link
Contributor

@CraigSiemens CraigSiemens commented Aug 31, 2023

Currently, a property with @TestState will have its value reset to nil before any afterEach in the same ExampleGroup is run. This is because:

  • the @TestState needs to be defined first so the tests can use it
  • the @TestState was adding an afterEach to the current ExampleGroup
  • the afterEach closures are run in the order they are defined in an ExampleGroup

This causes the afterEach resetting the TestState value to always be run before the afterEach closures defined in the same ExampleGroup.

This is an issue when a test needs to do additional cleanup using the value from TestState. Ideally the value in the TestState` would only be cleaned up after all the code in the test has finished running.

Example

@TestState var tempDir: URL!
        
beforeEach {
    tempDir = FileManager.default
        .temporaryDirectory
        .appendingPathComponent(randomDirName())
    try? FileManager.default.createDirectory(at: tempDir, withIntermediateDirectories: true)    
}

afterEach {
    try? FileManager.default.removeItem(at: tempDir)
}

When this runs, the afterEach will crash because tempDir has already been reset to nil.

Solution

TestState has been updated to use an afterEach set in a QuickConfiguration to ensure that the value is reset after the current example group has finished running. This works because configurations behave like a parent ExampleGroup of the root ExampleGroup for the spec.

On one hand this does feel a little wierd to for Quick itself to be defining a configuration, but it was the simplest approach I could find.

It's been updated to add a teardown block to the test case to set the value back to nil since that will always be run after the afterEach blocks.

Alternatives considered

Update ExampleGroup/ExampleHooks to a alwaysAfterEach array that would function similar to justBeforeEach where it's a second array of closures that are always run last. This would have been a larger change to add support to the Sync/Async versions of World, ExampleGroup, and ExampleHooks

Checklist

  • Does this have tests?
  • Does this have documentation?
  • Does this break the public API (Requires major version bump)?
  • Is this a new feature (Requires minor version bump)?

Copy link
Member

@younata younata left a comment

Choose a reason for hiding this comment

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

Thanks for this! Really appreciate it.

Also, would you like commit privileges to Quick and Nimble? You've been doing some great work, and I'd like to invite you to the Quick team.

@CraigSiemens
Copy link
Contributor Author

Also, would you like commit privileges to Quick and Nimble? You've been doing some great work, and I'd like to invite you to the Quick team.

I could take it, though I probably wouldn't do much with it until I felt more comfortable with the repo.

@CraigSiemens CraigSiemens requested a review from younata September 6, 2023 18:02
Copy link
Member

@younata younata left a comment

Choose a reason for hiding this comment

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

Sorry about the delay. This looks great, though. Thanks for doing this!

@younata younata enabled auto-merge September 11, 2023 15:15
@younata younata merged commit 55f2e48 into Quick:main Sep 11, 2023
@CraigSiemens CraigSiemens deleted the test-state-clearing-order branch September 11, 2023 20:44
cgrindel-self-hosted-renovate bot referenced this pull request in cgrindel/rules_swift_package_manager Sep 18, 2023
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [Quick/Quick](https://togithub.com/Quick/Quick) | minor | `from:
"7.2.0"` -> `from: "7.3.0"` |

---

### Release Notes

<details>
<summary>Quick/Quick (Quick/Quick)</summary>

### [`v7.3.0`](https://togithub.com/Quick/Quick/releases/tag/v7.3.0)

[Compare
Source](https://togithub.com/Quick/Quick/compare/v7.2.0...v7.3.0)

#### Highlights

- Adds a property wrapper default initializer for TestState. Meaning the
following declaration is now accepted!

```swift
@&#8203;TestState var foo: Int! = 30
```

Thanks [@&#8203;tahirmt](https://togithub.com/tahirmt)!

- TestState now nils out the value after all afterEach blocks run,
instead of in the middle of the afterEach chain. Thanks
[@&#8203;CraigSiemens](https://togithub.com/CraigSiemens)

#### What's Changed

- Bump activesupport from 7.0.4.3 to 7.0.7.2 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/Quick/Quick/pull/1238](https://togithub.com/Quick/Quick/pull/1238)
- Add property wrapper default initializer for TestState by
[@&#8203;tahirmt](https://togithub.com/tahirmt) in
[https://github.com/Quick/Quick/pull/1235](https://togithub.com/Quick/Quick/pull/1235)
- Bump actions/checkout from 3 to 4 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/Quick/Quick/pull/1241](https://togithub.com/Quick/Quick/pull/1241)
- Updated TestState to remove the value after the test. by
[@&#8203;CraigSiemens](https://togithub.com/CraigSiemens) in
[https://github.com/Quick/Quick/pull/1240](https://togithub.com/Quick/Quick/pull/1240)
- Update release script to generate a carthage binary and include it in
the release. by [@&#8203;younata](https://togithub.com/younata) in
[https://github.com/Quick/Quick/pull/1234](https://togithub.com/Quick/Quick/pull/1234)

#### New Contributors

- [@&#8203;tahirmt](https://togithub.com/tahirmt) made their first
contribution in
[https://github.com/Quick/Quick/pull/1235](https://togithub.com/Quick/Quick/pull/1235)

**Full Changelog**:
Quick/Quick@v7.2.0...v7.3.0

</details>

---

### Configuration

πŸ“… **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

β™» **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

πŸ‘» **Immortal**: This PR will be recreated if closed unmerged. Get
[config help](https://togithub.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi45Ni4wIiwidXBkYXRlZEluVmVyIjoiMzYuOTYuMCIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: Self-hosted Renovate Bot <361546+cgrindel-self-hosted-renovate[bot]@users.noreply.github.enterprise.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants