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

Add Tests and CI #17

Merged
merged 3 commits into from
Mar 21, 2023
Merged

Conversation

wardellbagby
Copy link
Contributor

This PR adds tests and continuous integration to this project. The commits themselves break down what they do better than I could summarize here but effectively, we add tests and make changes to the library that were necessary to make those tests possible, while also trying to modify the repo as little as possible. And a little bit of CI, as a bonus since I was already here.

I don't believe I've changed any functionality here but the logic around the changing of return to process.exit(1) is technically different behavior because process.exit(1) will always quit while return might quit. I explain the why of that change in the first commit as well.

fetch.js Outdated
@@ -1,6 +1,6 @@
const https = require("https");

module.exports = function fetch(options, data) {
module.exports = function fetch(options) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The "data" parameter looked unused by all usages of this function. I figured that was for eventual tests, so I removed it here since I did add tests.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, looks like the original author used data as a variable/object to hold fetch results output? https://github.com/dev-drprasad/delete-tag-and-release/blob/master/index.js#L76
But perhaps it could have just been named something more commonly seen like results? dunno what his ideas were, do you looking through that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think I figured out what happened there. data was meant for actually sending data along with the request. I'm going to un-delete that since it's not actually relevant to what I wanted to do: namely add tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

utils.js Outdated

// The fact that this uses the "createGetReleasesPath" is used in a test, so changes here should be reflected there as
// well.
const createDeleteReleasesPath = (releaseId, fullyQualifiedRepo = getRepoFromProcess()) => `${createGetReleasesPath(fullyQualifiedRepo)}/${releaseId}`
Copy link
Contributor Author

@wardellbagby wardellbagby Dec 16, 2022

Choose a reason for hiding this comment

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

The idea of fullyQualifiedRepo is that it's the owner plus the name of a repo, since that always uniquely identifies a GitHub repo. I liked that idea so it's used pretty extensively by the tests but to keep the changes as small as possible, I didn't change the actual Action code to use that same pattern, but we definitely could. owner and repo in lib.js are never used independently; only together. Makes sense to just combine them once and never bother with it again.

That being said, it should always be explicitly provided by lib.js when it calls one of the create functions in here; lib.js relying on getRepoFromProcess would be bad since the process.env can be changed during the Action's run. We don't care in tests, though since that would just fail the test.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can go ahead and change the Action code. I'd likely go for good testing (as you have done), and then we can do a prerelease of the action itself which signifies to community that it's not production ready (and so they can test and also give feedback).

The other thing is that I want to bump to eventually (maybe with all these changes) a major version like v2.x instead of the current v0.2.1 since the Action has indeed been used by thousands of repos now from our insights and the semver would be easier understood with a real number against MAJOR.MINOR.PATCH

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think bumping would be good. But not to v2.x, but v1.x. Bumping to 2.x.x isn't actually semver compatible; as far as I remember, all bumps have to be incremental by 1. So would have to bump to v1.0.0.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's semver compatible since semantic versioning doesn't say major version incremental by 1. It says each element must increase numerically. And for major version, if any backwards incompatible changes are introduced, it must be incremented; it doesn't say HOW much ;-). This has been brought up in the past, and covered in some of the FAQ Q/A like this one and a few others there: https://semver.org/#what-if-i-inadvertently-alter-the-public-api-in-a-way-that-is-not-compliant-with-the-version-number-change-ie-the-code-incorrectly-introduces-a-major-breaking-change-in-a-patch-release

Copy link
Collaborator

Choose a reason for hiding this comment

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

but maybe "by 1" would indeed be less confusing for the uninitiated, so we'll do that way when the time comes; agree.

@wardellbagby wardellbagby force-pushed the wardell/add-tests branch 2 times, most recently from 26c89f5 to 663a16c Compare December 17, 2022 05:54
@thadguidry
Copy link
Collaborator

@wardellbagby Hi again! Could you resolve the latest conflicts? And also, is your branch testable yet by me or someone else? or is it still somewhat of a WIP (work in progress) or Draft?

@wardellbagby
Copy link
Contributor Author

Hey @thadguidry! Yeah, I'll resolve the conflicts in a bit.

Yes, it's testable and once I resolve conflicts, it'll be mergable.

You can run the tests via npm run test.

package.json Show resolved Hide resolved
utils.js Outdated

// The fact that this uses the "createGetReleasesPath" is used in a test, so changes here should be reflected there as
// well.
const createDeleteReleasesPath = (releaseId, fullyQualifiedRepo = getRepoFromProcess()) => `${createGetReleasesPath(fullyQualifiedRepo)}/${releaseId}`
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can go ahead and change the Action code. I'd likely go for good testing (as you have done), and then we can do a prerelease of the action itself which signifies to community that it's not production ready (and so they can test and also give feedback).

The other thing is that I want to bump to eventually (maybe with all these changes) a major version like v2.x instead of the current v0.2.1 since the Action has indeed been used by thousands of repos now from our insights and the semver would be easier understood with a real number against MAJOR.MINOR.PATCH

Thoughts?

@wardellbagby wardellbagby force-pushed the wardell/add-tests branch 2 times, most recently from a323d92 to f3b7b2a Compare March 20, 2023 02:49
- Create tests for the action pertaining to deleting tags and releases.
This introduces Jest as a test runner and makes a few changes to make
testing easier, including:

  - Explicitly using process.exit instead of returns since Jest doesn't
  allow top-level returns and process.exit can be mocked.
  - Splitting out the code that runs the action from the code the
  contains the action code. This allows us to run the action in our test
  directly and wait for the action run to finish by awaiting the
  promise it returns instead of just requiring the file.
  - Creating utility functions for creating the paths that are used to
  hit the GitHub API. This allows us to make assertions that fetch is
  called with the correct path and data while having to do as little
  string matching as possible, since that tends to be brittle.
  - Creating a "mock" fetch. This will be automatically used by Jest
  when we use jest.mock('./fetch.js') in a test since it's in the
   __mocks__ directory. This keeps us from making actual network calls
   when running tests and lets us test things like how the action
   behaves when it gets errors.
- The package-lock.json file tells NPM (and other package managers that
support it) exactly what version of NPM packages are in use by a
project. By including it into the repo, we can make sure that all
contributors are using the exact same version of dependencies.
- This adds a GitHub Action that will run on every new PR that is
pointed towards the master branch to run the test suite. Tests will run
against Node 16 and Node 18.
@wardellbagby
Copy link
Contributor Author

@thadguidry Alright, I think I've address everything now.

@thadguidry
Copy link
Collaborator

@thadguidry Alright, I think I've address everything now.

awesome! so I'll checkout and run npm run test

@thadguidry
Copy link
Collaborator

E:\GitHubRepos\delete-tag-and-release>npm run test

> [email protected] test
> jest index.spec.js

'jest' is not recognized as an internal or external command,
operable program or batch file.

E:\GitHubRepos\delete-tag-and-release>

@wardellbagby
Copy link
Contributor Author

wardellbagby commented Mar 20, 2023

E:\GitHubRepos\delete-tag-and-release>npm run test

> [email protected] test
> jest index.spec.js

'jest' is not recognized as an internal or external command,
operable program or batch file.

E:\GitHubRepos\delete-tag-and-release>

@thadguidry since there's a new dependency added, you have to run npm install first. Apologies; I should've mentioned that.

@thadguidry thadguidry mentioned this pull request Mar 21, 2023
@thadguidry
Copy link
Collaborator

@wardellbagby OK, the local testing passed.
Now, hmm, that is all mocked. Great! But what would be your ideas or plan to remain in sync with the GitHub API (since your mocking a portion of the API responses, yeah?) How do other Actions in the Marketplace do that if they also use mocking?

@thadguidry
Copy link
Collaborator

@constgen Do you happen to have insight on perhaps how the testing/mocking might get better also?

@wardellbagby
Copy link
Contributor Author

Don't worry @thadguidry, I plan on following this PR up with one that ports it to using the Octokit libraries and that'll rewrite these tests as well.

I want this PR merged as the "safe" version that doesn't change any functionality so that the refactor will have a good tested base to compare to.

I'll also update the README as well with development instructions and implement Prettier and eslint for solid source code management.

@thadguidry thadguidry merged commit 6a81278 into dev-drprasad:master Mar 21, 2023
This pull request was closed.
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