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

Exit jrnl if no text entered into editor #744

Merged
merged 28 commits into from
Nov 19, 2019
Merged

Exit jrnl if no text entered into editor #744

merged 28 commits into from
Nov 19, 2019

Conversation

alichtman
Copy link
Contributor

@alichtman alichtman commented Nov 14, 2019

Fix #589

Pretty self-explanatory. I was trying to think of a creative way to test this but I don't think we can automate exiting the editor cleanly (and judging by the lack of tests for anything relating to an editor, I think everyone else has come to the same conclusion that I did.)

Checklist

  • The code change is tested and works locally.
  • Tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?

@pspeter
Copy link
Contributor

pspeter commented Nov 14, 2019

I see two ways to test this (and other related functionality):

  1. (easy) Mock the whole util.get_text_from_editor() function to return a predefined string ("" in this case)
  2. (bit harder) Mock the subprocess.call() call to put that predefined string into the tmpfile.

@alichtman
Copy link
Contributor Author

alichtman commented Nov 15, 2019

Mock the whole util.get_text_from_editor() function to return a predefined string ("" in this case)

This solution makes sense to me. I'm thinking this is somewhat along the lines of how I'll implement it, but I'm a little shaky on the details. (The following is left as notes for myself when I have time to finish thinking about this.)

The feature test may look like this:

 Scenario: Writing an empty entry from the editor
        Given we use the config "basic.yaml"
        When we open the editor and exit
        Then there should be no output

Where open the editor and exit will correspond to a @when decorator in steps/core.py that does something after a line with unittest.patch('util.get_text_from_editor', side_effect=return_empty_string).

Note that this would mean the test will require no output, when in reality, the output would be [Nothing saved to file].

Note: I'm not sure that the test I've written actually does anything at the moment...

Copy link
Contributor

@pspeter pspeter left a comment

Choose a reason for hiding this comment

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

I would be a bigger fan of patching the concrete subprocess call. Generally you should avoid patching your own code, especially if it has side-effects (like the printed output). I mentioned it earlier because it was easier but I didn't consider it would change the output.

features/steps/core.py Outdated Show resolved Hide resolved
features/steps/core.py Outdated Show resolved Hide resolved
features/steps/core.py Outdated Show resolved Hide resolved
@alichtman
Copy link
Contributor Author

Yep, agreed. Will fix when I've got some time.

@alichtman
Copy link
Contributor Author

alichtman commented Nov 16, 2019

It's unclear to me why this test outputs the following on stderr:

image

On this branch, when I open the editor, write nothing and exit, I get this:

image

Edit: Haha, so it passes on Travis? I hate computers sometimes.

image

features/steps/core.py Outdated Show resolved Hide resolved
features/steps/core.py Outdated Show resolved Hide resolved
jrnl/cli.py Outdated Show resolved Hide resolved
features/steps/core.py Outdated Show resolved Hide resolved
alichtman and others added 2 commits November 16, 2019 14:35
Co-Authored-By: pspeter <peter.schmidb@gmail.com>
@pspeter
Copy link
Contributor

pspeter commented Nov 16, 2019

There we go :D

pspeter
pspeter previously approved these changes Nov 16, 2019
Copy link
Contributor

@pspeter pspeter left a comment

Choose a reason for hiding this comment

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

It's not super necessary I guess but after moving the sys.exit() down, you could also add a second test that is exactly the same as this one but uses the basic.yaml and When we run jrnl and enter "" (and add @when('we run "{command}" and enter ""') to the corresponding step function...) to also test the early exit when no editor is used.

But otherwise it looks good now 👍

@alichtman
Copy link
Contributor Author

I honestly don't think that's necessary. Who's going to be running jrnl ""?

@pspeter
Copy link
Contributor

pspeter commented Nov 17, 2019

"" denotes an empty string. It's the same scenario as above when somebody closes the editor without writing anything, only without the editor. But without an editor, this line will be called:

raw = sys.stdin.read()

But this can also be exited without writing anything, leaving you with an empty string again. They would still call jrnl without quotes.

@alichtman
Copy link
Contributor Author

alichtman commented Nov 17, 2019

Oh, so this would happen if someone tried to cat an empty file into jrnl on stdin?

@pspeter
Copy link
Contributor

pspeter commented Nov 17, 2019

That too, I assume. But even if you don't pipe anything into jrnl, it will start an input stream at that line and let you type something in your terminal. You can input multiple lines and then stop the stream with ctrl+d.

grafik

@alichtman
Copy link
Contributor Author

This test breaks for some unknown reason. Will take a look at it again when I have time. I have to do some other work...

Copy link
Contributor

@pspeter pspeter left a comment

Choose a reason for hiding this comment

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

Try this

features/core.feature Outdated Show resolved Hide resolved
alichtman and others added 3 commits November 17, 2019 02:07
Co-Authored-By: pspeter <peter.schmidb@gmail.com>
@alichtman
Copy link
Contributor Author

@wren: This PR is also ready for review.

@wren wren added this to the v2.1.2 - Non-critical bug fixes milestone Nov 19, 2019
@wren wren changed the base branch from master to v2.1.2 November 19, 2019 05:45
@wren wren changed the base branch from v2.1.2 to master November 19, 2019 06:05
Copy link
Member

@wren wren left a comment

Choose a reason for hiding this comment

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

💯

@wren wren merged commit 0bd94c7 into jrnl-org:master Nov 19, 2019
@alichtman alichtman deleted the exit-after-no-text-entry branch November 19, 2019 12:20
@wren wren added the bug Something isn't working label Jan 10, 2020
@lock
Copy link

lock bot commented May 21, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot added the 🔒 Outdated label May 21, 2020
@lock lock bot locked as resolved and limited conversation to collaborators May 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working 🔒 Outdated
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants