Skip to content

Adds function to create stash entry - #7085

Closed
iAmWillShepherd wants to merge 31 commits into
developmentfrom
create-stash
Closed

iAmWillShepherd wants to merge 31 commits into
developmentfrom
create-stash

Conversation

@iAmWillShepherd

@iAmWillShepherd iAmWillShepherd commented Mar 14, 2019 •

Copy link
Copy Markdown
Contributor

Adds function that creates a stash entry.

See tracking issue: #6987

@iAmWillShepherd iAmWillShepherd mentioned this pull request Mar 14, 2019
6 tasks done
@shiftkey
shiftkey changed the base branch from development to all-the-stashes March 14, 2019 14:29
Comment thread app/test/unit/git/stash-test.ts Outdated
await GitProcess.exec(['commit', '-m', 'initial commit'], repository.path)
})

it('creates a stash entry', async () => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@shiftkey
shiftkey changed the base branch from all-the-stashes to development March 19, 2019 13:30
@shiftkey

Copy link
Copy Markdown
Member

As #7032 has been merged I changed the base branch back to development to indicate this is unblocked.

@iAmWillShepherd

Copy link
Copy Markdown
Contributor Author

🍎

@iAmWillShepherd iAmWillShepherd added the ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Mar 20, 2019
@shiftkey shiftkey self-assigned this Mar 20, 2019
Comment thread app/src/lib/git/stash.ts Resolved
Comment thread app/src/lib/git/stash.ts Resolved
Comment thread app/test/unit/git/stash-test.ts Outdated
async function getTipSha(repository: Repository) {
const result = await GitProcess.exec(['rev-parse', 'HEAD'], repository.path)
const tipSha = result.stdout.trim()
return result.stdout.trim()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 createStashMessage with 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:

Screen Shot 2019-03-20 at 10 05 10 AM

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And I see now this was partially addressed in a61292d but the rest of the points are still relevant

Comment thread app/test/unit/git/stash-test.ts Resolved
Comment thread app/test/unit/git/stash-test.ts Resolved
@shiftkey shiftkey removed their assignment Mar 23, 2019
@shiftkey

Copy link
Copy Markdown
Member

Looks like this test needs to be reviewed:

FAIL app/test/unit/git/stash-test.ts
  ● git/stash › getDesktopStashEntries › handles unborn repo by returning empty list

    Unable to find commit for HEAD - check that you are not working with an unborn repository

       7 | 
       8 |   if (commit === null) {
    >  9 |     throw new Error(
         |           ^
      10 |       'Unable to find commit for HEAD - check that you are not working with an unborn repository'
      11 |     )
      12 |   }

      at Object.getTipOrError (test/helpers/tip.ts:9:11)

@iAmWillShepherd iAmWillShepherd removed the ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Mar 28, 2019
@iAmWillShepherd

Copy link
Copy Markdown
Contributor Author

Removed Ready for review label while I investigate why my tests only fail on Windows.

@iAmWillShepherd

Copy link
Copy Markdown
Contributor Author

It seems my tests are failing due to me inspecting the GitError thrown as a result of calling createDesktopStashEntry

it('fails when repository is unborn', async () => {
repository = await setupEmptyRepository()
await FSE.writeFile(readme, '')
let didFail = false
try {
await createDesktopStashEntry(repository, 'master', 'some_sha')
} catch (e) {
if (e instanceof GitError) {
if (
e.result.stderr.indexOf('fatal: Needed a single revision') !== -1
) {
didFail = true
}
}
}
expect(didFail).toBe(true)
})

☝️Fails on Windows because I was inspecting stderr for the string "fatal: Needed a single revision!". That is not returned on Windows.

Windows
Windows

macOS
Screen Shot 2019-03-28 at 11 11 55 AM

it('fails when repository is in conflicted state', async () => {
repository = await setupConflictedRepo()
await FSE.appendFile(readme, 'just testing stuff')
const tipCommit = await getTipOrError(repository)
let didFail = false
try {
await createDesktopStashEntry(repository, 'master', tipCommit.sha)
} catch (e) {
if (e instanceof GitError) {
if (
e.message.indexOf('fatal: git-write-tree: error building trees') !==
-1
) {
didFail = true
}
}
}
expect(didFail).toBe(true)
})

☝️Fails on Windows for the same reason as above.

Windows
Screen Shot 2019-03-28 at 11 27 04 AM

macOS
Screen Shot 2019-03-28 at 11 26 50 AM

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.

@iAmWillShepherd

Copy link
Copy Markdown
Contributor Author

Closing in favor of #7182.

@iAmWillShepherd
iAmWillShepherd deleted the create-stash branch April 5, 2019 15:22
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