Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Adding a dry run flag #5

Merged
merged 3 commits into from
Aug 25, 2016
Merged

Adding a dry run flag #5

merged 3 commits into from
Aug 25, 2016

Conversation

seemethere
Copy link
Contributor

Adds a --dry-run flag that outputs all output to stdout.

Example usage:

$ undebt -p undebt/examples/method_to_function.py  -i tests/inputs/method_to_function_input.txt --dry-run

Example output:

>>> tests/inputs/method_to_function_input.txt
from function_lives_here import function
something before code pattern
@decorator([Class])
def some_function(self, abc, xyz):
    """herp the derp while also derping and herping"""
    cde = fgh(self.l)
    ijk = cde.herp(
        opq_foo=function(FOO(abc))
    )['str']
    lmn = cde.herp(
        opq_foo=function(FOO(xyz))
    )['str']
bla bla bla
    for str_data in derp_data['data']['strs']:
        if str_data['str'] == Type.HERP:
            disable(herp_foo=herp_foo, derp_foo=derp_foo)
            rst.uvw(
                CTA_BUSINESS_PLATFORM_DISABLED_LOG,
                "derp {derp_foo} herp {herp_foo}".format(
                    derp_foo=function(FOO(derp_foo)),
                    herp_foo=function(FOO(herp_foo)),
                ),
            )
something after code pattern

Related to issue #3

@evhub
Copy link
Contributor

evhub commented Aug 23, 2016

@seemethere Looks good! One major thing, though—it'd be nice to have some tests for this. Can you add a test to main_test.py that uses --dry-run? Then run make test to ensure it (and all the other tests) are passing.

@seemethere
Copy link
Contributor Author

@evhub I was just thinking about that too!

@coveralls
Copy link

coveralls commented Aug 24, 2016

Coverage Status

Changes Unknown when pulling 2df4c11 on seemethere:dry_run into * on Yelp:master*.

@seemethere
Copy link
Contributor Author

@evhub Tests have been added!

@@ -6,6 +6,7 @@
import os.path

import mock
from six import StringIO
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather not add a new dependency just to get StringIO. Can you do

try:
    from io import StringIO
except ImportError:
    from StringIO import StringIO

instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't that exactly what six does?

I agree with leaning against adding dependencies, but six seems to be a pretty standard one, especially for ensuring 2/3 compat

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, fair point—I retract my request to remove six. If we're using six here, though, we should probably use it elsewhere also—but I can take care of that after this is merged.

Copy link
Contributor

Choose a reason for hiding this comment

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

imo use six, it's what it's for

But in this case you can avoid it entirely, I'll comment below :D

Copy link
Contributor

Choose a reason for hiding this comment

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

and actually, this is just a test so it shouldn't have been added as a dependency in setup.py (setup.py is for runtime dependencies, whereas this is a test-time dependency (and therefore belongs in requirements-dev.txt))

@evhub
Copy link
Contributor

evhub commented Aug 24, 2016

LGTM!

@asottile Do you want to weigh in here before I merge this?

@@ -62,6 +63,13 @@ def test_directory():
assert _read_input_file() == method_to_function_output_contents == _read_output_file()


def test_dry_run():
Copy link
Contributor

Choose a reason for hiding this comment

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

@asottile
Copy link
Contributor

@asottile Do you want to weigh in here before I merge this?

eheh, didn't meant to get terribly involved in the project but sure :D

@Daenyth
Copy link

Daenyth commented Aug 25, 2016

I haven't used undebt yet, but one thing that would make this specific thing more useful to me is a diff-mode output. As it stands, I'd have to either not use the dry run option and use git for my diff, or I use dry run, and pipe it to a file to diff manually.

@asottile
Copy link
Contributor

@Daenyth that's a great idea. Most of these fixer type tools (especially the ones we use with pre-commit like my import reorderer, yapf, autopep8, etc.) have prior art.

Want to open an additional issue?

@Daenyth
Copy link

Daenyth commented Aug 25, 2016

For dry-run to have a "show diff" variant? Sure

@seemethere
Copy link
Contributor Author

@evhub Removed six dependency, switch to capsys

@coveralls
Copy link

coveralls commented Aug 25, 2016

Coverage Status

Changes Unknown when pulling 04ebc77 on seemethere:dry_run into * on Yelp:master*.

2 similar comments
@coveralls
Copy link

Coverage Status

Changes Unknown when pulling 04ebc77 on seemethere:dry_run into * on Yelp:master*.

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling 04ebc77 on seemethere:dry_run into * on Yelp:master*.

def test_dry_run(capsys):
args = ["undebt", "-i", method_to_function_input_path, "-p", method_to_function_path, "--verbose", "--dry-run"]
with mock.patch("sys.argv", args):
main()
Copy link
Contributor

Choose a reason for hiding this comment

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

We us this pattern in a lot of other repositories (could make this so you don't need monkeypatching):

def main(argv=None):
    parser = argparse.ArgumentParser()
    ...
    parser.pase_args(argv)

By default, argparse will go to sys.argv if the passed in argument is None otherwise it'll use the list of arguments.

This lets you call main(('-i', ..., ..., ...)) under test

Copy link
Contributor

Choose a reason for hiding this comment

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

@evhub
Copy link
Contributor

evhub commented Aug 25, 2016

I'm going to merge this and then try to address all of the issues brought up in this thread.

@evhub evhub merged commit bba4893 into Yelp:master Aug 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants