fossa-cli icon indicating copy to clipboard operation
fossa-cli copied to clipboard

make matchExpectations pattern match everything so that there is no p…

Open spatten opened this issue 3 years ago • 0 comments

Overview

I lost some time trying to figure out why my API expectations were failing when the request being sent was exactly what I expected it to be.

It turned out that the problem was that I had forgotten to mock the endpoint.

This PR avoids future me having to scratch his head for 10 minutes by making the MockAPI pattern matching complete without the matchExpectation _ _ = Nothing line. This way if you forget to mock a request, HLS will let you know.

Acceptance criteria

  • The tests still pass
  • If you forget to mock an endpoint, HLS will let you know that you have incomplete pattern matching

Testing plan

Run the unit tests. They should all still pass

make test

Edit ArchiveUploaderSpec.hs. Comment out line 42:

--      expectQueueArchiveBuild Fixtures.firstArchive

Run the tests again

make test ARGS="ArchiveUploader"

The results should show that the failure was because there was no matching request:

  test/Test/MockApi.hs:174:18: 
  1) App.Fossa.ArchiveUploader.archiveUploadSourceUnit should do the archive upload workflow for multiple archives
       Unexpected call: 
         NoMatchingRequest: QueueArchiveBuild (Archive {archiveName = "second-archive-test", archiveVersion = "0.0.1"})
       Unsatisfied expectations: 
         GetOrganization
         GetApiOpts
         GetSignedUploadUrl (PackageRevision {packageName = "first-archive-test", packageVersion = "0.0.1"})
         GetSignedUploadUrl (PackageRevision {packageName = "second-archive-test", packageVersion = "0.0.1"})

Revert that change to ArchiveUploaderSpec. Now comment out where we mock out QueueArchiveBuild on line 219 of MockApi.hs:

-- matchExpectation a@(QueueArchiveBuild{}) (ApiExpectation _ requestExpectation b@(QueueArchiveBuild{}) resp) = checkResult requestExpectation a b resp

Run the tests again. You should see failures, and it should be clear that they're due to an unmocked endpoint:

 test/Test/MockApi.hs:174:18: 
  1) App.Fossa.ArchiveUploader.archiveUploadSourceUnit should do the archive upload workflow for a single archive
       Unexpected call: 
         UnmockedEndpoint: QueueArchiveBuild (Archive {archiveName = "first-archive-test", archiveVersion = "0.0.1"})
       Unsatisfied expectations: 
         GetOrganization
         GetApiOpts
         GetSignedUploadUrl (PackageRevision {packageName = "first-archive-test", packageVersion = "0.0.1"})
         QueueArchiveBuild (Archive {archiveName = "first-archive-test", archiveVersion = "0.0.1"})

Risks

References

Checklist

spatten avatar Jun 29 '22 00:06 spatten