|
| 1 | +# pylint: disable=missing-module-docstring,missing-function-docstring,too-many-lines |
| 2 | +import textwrap |
| 3 | + |
| 4 | +from saltrewrite.salt import fix_dunder_utils |
| 5 | + |
| 6 | + |
| 7 | +def test_fix_call_one_arg(tempfiles): |
| 8 | + code = textwrap.dedent( |
| 9 | + """ |
| 10 | + import one.two |
| 11 | +
|
| 12 | + def one(): |
| 13 | + one.two.three("four") |
| 14 | + __utils__["foo.bar"]("one") |
| 15 | + """ |
| 16 | + ) |
| 17 | + expected_code = textwrap.dedent( |
| 18 | + """ |
| 19 | + import one.two |
| 20 | + import salt.utils.foo |
| 21 | +
|
| 22 | + def one(): |
| 23 | + one.two.three("four") |
| 24 | + salt.utils.foo.bar("one") |
| 25 | + """ |
| 26 | + ) |
| 27 | + fpath = tempfiles.makepyfile(code, prefix="test_") |
| 28 | + fix_dunder_utils.rewrite(fpath) |
| 29 | + with open(fpath) as rfh: |
| 30 | + new_code = rfh.read() |
| 31 | + assert new_code == expected_code |
| 32 | + |
| 33 | + |
| 34 | +def test_fix_call_multiple_args(tempfiles): |
| 35 | + code = textwrap.dedent( |
| 36 | + """ |
| 37 | + import one.two |
| 38 | +
|
| 39 | + def one(): |
| 40 | + one.two.three("four") |
| 41 | + __utils__["foo.bar"]("one", True, 1, 2.0) |
| 42 | + """ |
| 43 | + ) |
| 44 | + expected_code = textwrap.dedent( |
| 45 | + """ |
| 46 | + import one.two |
| 47 | + import salt.utils.foo |
| 48 | +
|
| 49 | + def one(): |
| 50 | + one.two.three("four") |
| 51 | + salt.utils.foo.bar("one", True, 1, 2.0) |
| 52 | + """ |
| 53 | + ) |
| 54 | + fpath = tempfiles.makepyfile(code, prefix="test_") |
| 55 | + fix_dunder_utils.rewrite(fpath) |
| 56 | + with open(fpath) as rfh: |
| 57 | + new_code = rfh.read() |
| 58 | + assert new_code == expected_code |
| 59 | + |
| 60 | + |
| 61 | +def test_fix_call_keyword_arguments(tempfiles): |
| 62 | + code = textwrap.dedent( |
| 63 | + """ |
| 64 | + import one.two |
| 65 | +
|
| 66 | + def one(): |
| 67 | + one.two.three("four") |
| 68 | + __utils__["foo.bar"](one="one") |
| 69 | + """ |
| 70 | + ) |
| 71 | + expected_code = textwrap.dedent( |
| 72 | + """ |
| 73 | + import one.two |
| 74 | + import salt.utils.foo |
| 75 | +
|
| 76 | + def one(): |
| 77 | + one.two.three("four") |
| 78 | + salt.utils.foo.bar(one="one") |
| 79 | + """ |
| 80 | + ) |
| 81 | + fpath = tempfiles.makepyfile(code, prefix="test_") |
| 82 | + fix_dunder_utils.rewrite(fpath) |
| 83 | + with open(fpath) as rfh: |
| 84 | + new_code = rfh.read() |
| 85 | + assert new_code == expected_code |
| 86 | + |
| 87 | + |
| 88 | +def test_fix_call_multiple_keyword_arguments(tempfiles): |
| 89 | + code = textwrap.dedent( |
| 90 | + """ |
| 91 | + import one.two |
| 92 | +
|
| 93 | + def one(): |
| 94 | + one.two.three("four") |
| 95 | + __utils__["foo.bar"]( |
| 96 | + one="one", |
| 97 | + two="two" |
| 98 | + ) |
| 99 | + """ |
| 100 | + ) |
| 101 | + expected_code = textwrap.dedent( |
| 102 | + """ |
| 103 | + import one.two |
| 104 | + import salt.utils.foo |
| 105 | +
|
| 106 | + def one(): |
| 107 | + one.two.three("four") |
| 108 | + salt.utils.foo.bar( |
| 109 | + one="one", |
| 110 | + two="two") |
| 111 | + """ |
| 112 | + ) |
| 113 | + fpath = tempfiles.makepyfile(code, prefix="test_") |
| 114 | + fix_dunder_utils.rewrite(fpath) |
| 115 | + with open(fpath) as rfh: |
| 116 | + new_code = rfh.read() |
| 117 | + assert new_code == expected_code |
| 118 | + |
| 119 | + |
| 120 | +def test_fix_call_mixed(tempfiles): |
| 121 | + code = textwrap.dedent( |
| 122 | + """ |
| 123 | + import one.two |
| 124 | +
|
| 125 | + def one(): |
| 126 | + one.two.three("four") |
| 127 | + __utils__["foo.bar"]("one", two="two") |
| 128 | + """ |
| 129 | + ) |
| 130 | + expected_code = textwrap.dedent( |
| 131 | + """ |
| 132 | + import one.two |
| 133 | + import salt.utils.foo |
| 134 | +
|
| 135 | + def one(): |
| 136 | + one.two.three("four") |
| 137 | + salt.utils.foo.bar("one", two="two") |
| 138 | + """ |
| 139 | + ) |
| 140 | + fpath = tempfiles.makepyfile(code, prefix="test_") |
| 141 | + fix_dunder_utils.rewrite(fpath) |
| 142 | + with open(fpath) as rfh: |
| 143 | + new_code = rfh.read() |
| 144 | + assert new_code == expected_code |
0 commit comments