Repository navigation
Adds function to create stash entry - #7085
iAmWillShepherd wants to merge 31 commits into
Conversation
| await GitProcess.exec(['commit', '-m', 'initial commit'], repository.path) | ||
| }) | ||
|
|
||
| it('creates a stash entry', async () => { |
There was a problem hiding this comment.
I can add additional tests to cover scenarios like being in the middle of a rebase or merge, but I'm not sure of the value since we do a pretty good job of not allowing actions to take place on invalid repos.
There was a problem hiding this comment.
I'd try it out purely from a "what happens" scenario given the app might not always prevent the user from doing things - for example #6429 lets users try and merge when already in a merge, and it fails with some helpful context - and it'd be good to ensure your code behaves in a known & predictable way.
Try it out using a repository in the middle of a merge conflict (we should have scaffolding for this) - you might find yourself saying "uh-oh, we shouldn't allow any stashing at this time because XYZ" which would influence what your functions do as pre-conditions to doing stash-related work.
|
As #7032 has been merged I changed the base branch back to |
|
🍎 |
| async function getTipSha(repository: Repository) { | ||
| const result = await GitProcess.exec(['rev-parse', 'HEAD'], repository.path) | ||
| const tipSha = result.stdout.trim() | ||
| return result.stdout.trim() |
There was a problem hiding this comment.
There's a problem with trusting the Git process output implicitly that I wanted to call out while we're in here making changes to the test suite.
Before looking at these new tests here I added this line to development and ran the tests again:
diff --git a/app/test/unit/git/stash-test.ts b/app/test/unit/git/stash-test.ts
index 5af95ebdd..21e8a6633 100644
--- a/app/test/unit/git/stash-test.ts
+++ b/app/test/unit/git/stash-test.ts
@@ -50,6 +50,7 @@ describe('git/stash', () => {
async function stash(repository: Repository, message?: string) {
const result = await GitProcess.exec(['rev-parse', 'HEAD'], repository.path)
+ expect(result.exitCode).toBe(0)
const tipSha = result.stdout.trim()
await GitProcess.exec(
['stash', 'push', '-m', message || createStashMessage('master', tipSha)],I was able to make one of the tests fail:
Summary of all failing tests
FAIL test/unit/git/stash-test.ts (7.378s)
● git/stash › getDesktopStashEntries › handles unborn repo by returning empty list
expect(received).toBe(expected) // Object.is equality
Expected: 0
Received: 128
51 | async function stash(repository: Repository, message?: string) {
52 | const result = await GitProcess.exec(['rev-parse', 'HEAD'], repository.path)
> 53 | expect(result.exitCode).toBe(0)
| ^
54 | const tipSha = result.stdout.trim()
55 | await GitProcess.exec(
56 | ['stash', 'push', '-m', message || createStashMessage('master', tipSha)],
at stash (test/unit/git/stash-test.ts:53:27)
This makes me feel the original test could be better organized:
- we were interested in testing
getDesktopStashEntries - but the test also creates a stash before doing this
- it uses
git rev-parse HEAD(which errors because it's an unborn repository) - but then it calls
createStashMessagewith this empty string for a SHA and completes without error (we should test for that to ensure we're getting the right inputs we expect 🎉) - and then the stash operation errors anyway because it's using an unborn repository
- and it turns out we didn't need to do this test setup anyway
To make the point in bold extra clear, I added a dumb assertion to createStashMessage and was able to see the underlying problem pretty quickly:
Summary of all failing tests
FAIL test/unit/git/stash-test.ts (14.478s)
● git/stash › getDesktopStashEntries › handles unborn repo by returning empty list
Tip found 'HEAD' has length 4 which is not the expected 40 characters
82 | export function createStashMessage(branchName: string, tipSha: string) {
83 | if (tipSha.length !== 40) {
> 84 | throw new Error(
| ^
85 | `Tip found '${tipSha}' has length ${
86 | tipSha.length
87 | } which is not the expected 40 characters`
at Object.createStashMessage (src/lib/git/stash.ts:84:11)
at stash (test/unit/git/stash-test.ts:55:40)
Which is oddly just what the command line outputs:
Making your test setup fail when unexpected exit codes are encountered is something I'd recommend doing for your tests. GitProcess.exec is a very sharp tool, and without the application error handling it's up to you to have a healthy distrust of what it's doing in your tests.
I'd recommend baking assertions into your test setup to clarify what you expect to happen. For example, this could become the stash function (using the getTipOrError helper function):
async function stash(repository: Repository, message?: string) {
if (message === undefined) {
// create a stash message using the current repository state
const tip = await getTipOrError(repository)
message = createStashMessage('master', tip.sha)
}
const result = await GitProcess.exec(
['stash', 'push', '-m', message],
repository.path
)
// stash in test setup should always complete without error
expect(result.exitCode).toBe(0)
}And then back in the test these lines can be omitted because they weren't important to the test itself:
diff --git a/app/test/unit/git/stash-test.ts b/app/test/unit/git/stash-test.ts
index 5af95ebdd..9693b6c35 100644
--- a/app/test/unit/git/stash-test.ts
+++ b/app/test/unit/git/stash-test.ts
@@ -24,9 +24,6 @@ describe('git/stash', () => {
it('handles unborn repo by returning empty list', async () => {
const repo = await setupEmptyRepository()
let didFail = false
- readme = path.join(repo.path, 'README.md')
- await FSE.writeFile(readme, '')
- await stash(repo)
try {
await getDesktopStashEntries(repo)There was a problem hiding this comment.
And I see now this was partially addressed in a61292d but the rest of the points are still relevant
Co-Authored-By: iAmWillShepherd <[email protected]>
|
Looks like this test needs to be reviewed: |
|
Removed |
|
It seems my tests are failing due to me inspecting the desktop/app/test/unit/git/stash-test.ts Lines 67 to 85 in 6035715 ☝️Fails on Windows because I was inspecting desktop/app/test/unit/git/stash-test.ts Lines 87 to 107 in 6035715 ☝️Fails on Windows for the same reason as above. The underlying problem is I'm trying to figure out what type of error I have to make sure it's failing the right way. I think I will remove this error inspection so I can move forward. |
Due to the differences in how errors are reported, doing a naive comparison of the error message is not sufficient. See #7085 (comment) for more details.
All git errors aren't necessarily an error Desktop needs to care about. In those instances, just return an empty list.
|
Closing in favor of #7182. |





Adds function that creates a stash entry.
See tracking issue: #6987