| 1 |
"""Check the imports parsing.""" |
| 2 |
import io |
| 3 |
|
| 4 |
from logassert import Exact |
| 5 |
|
| 6 |
from fades import parsing, REPO_PYPI, REPO_VCS |
| 7 |
|
| 8 |
from tests import get_reqs |
| 9 |
|
| 10 |
|
| 11 |
def test_nocomment(): |
| 12 |
# note that we're testing the import at the beginning of the line, and |
| 13 |
# in also indented |
| 14 |
parsed = parsing._parse_content(io.StringIO("""import time |
| 15 |
import time |
| 16 |
from time import foo |
| 17 |
""")) |
| 18 |
assert parsed == {} |
| 19 |
|
| 20 |
|
| 21 |
def test_simple_default(): |
| 22 |
parsed = parsing._parse_content(io.StringIO(""" |
| 23 |
import time |
| 24 |
import foo # fades |
| 25 |
""")) |
| 26 |
assert parsed == {REPO_PYPI: get_reqs('foo')} |
| 27 |
|
| 28 |
|
| 29 |
def test_double(): |
| 30 |
parsed = parsing._parse_content(io.StringIO(""" |
| 31 |
import time # fades |
| 32 |
import foo # fades |
| 33 |
""")) |
| 34 |
assert parsed == { |
| 35 |
REPO_PYPI: get_reqs('time') + get_reqs('foo') |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
def test_version_same_default(): |
| 40 |
parsed = parsing._parse_content(io.StringIO(""" |
| 41 |
import foo # fades == 3.5 |
| 42 |
""")) |
| 43 |
assert parsed == { |
| 44 |
REPO_PYPI: get_reqs('foo == 3.5') |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
def test_version_different(): |
| 49 |
parsed = parsing._parse_content(io.StringIO(""" |
| 50 |
import foo # fades !=3.5 |
| 51 |
""")) |
| 52 |
assert parsed == { |
| 53 |
REPO_PYPI: get_reqs('foo !=3.5') |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
def test_version_same_no_spaces(): |
| 58 |
parsed = parsing._parse_content(io.StringIO(""" |
| 59 |
import foo # fades==3.5 |
| 60 |
""")) |
| 61 |
assert parsed == { |
| 62 |
REPO_PYPI: get_reqs('foo ==3.5') |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
def test_version_same_two_spaces(): |
| 67 |
parsed = parsing._parse_content(io.StringIO(""" |
| 68 |
import foo # fades == 3.5 |
| 69 |
""")) |
| 70 |
assert parsed == { |
| 71 |
REPO_PYPI: get_reqs('foo == 3.5') |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
def test_version_same_one_space_before(): |
| 76 |
parsed = parsing._parse_content(io.StringIO(""" |
| 77 |
import foo # fades == 3.5 |
| 78 |
""")) |
| 79 |
assert parsed == { |
| 80 |
REPO_PYPI: get_reqs('foo == 3.5') |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
def test_version_same_two_space_before(): |
| 85 |
parsed = parsing._parse_content(io.StringIO(""" |
| 86 |
import foo # fades == 3.5 |
| 87 |
""")) |
| 88 |
assert parsed == { |
| 89 |
REPO_PYPI: get_reqs('foo == 3.5') |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
def test_version_same_one_space_after(): |
| 94 |
parsed = parsing._parse_content(io.StringIO(""" |
| 95 |
import foo # fades== 3.5 |
| 96 |
""")) |
| 97 |
assert parsed == { |
| 98 |
REPO_PYPI: get_reqs('foo == 3.5') |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
def test_version_same_two_space_after(): |
| 103 |
parsed = parsing._parse_content(io.StringIO(""" |
| 104 |
import foo # fades== 3.5 |
| 105 |
""")) |
| 106 |
assert parsed == { |
| 107 |
REPO_PYPI: get_reqs('foo == 3.5') |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
def test_version_greater(): |
| 112 |
parsed = parsing._parse_content(io.StringIO(""" |
| 113 |
import foo # fades > 2 |
| 114 |
""")) |
| 115 |
assert parsed == { |
| 116 |
REPO_PYPI: get_reqs('foo > 2') |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
def test_version_greater_no_space(): |
| 121 |
parsed = parsing._parse_content(io.StringIO(""" |
| 122 |
import foo # fades>2 |
| 123 |
""")) |
| 124 |
assert parsed == { |
| 125 |
REPO_PYPI: get_reqs('foo >2') |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
def test_version_greater_no_space_default(): |
| 130 |
parsed = parsing._parse_content(io.StringIO(""" |
| 131 |
import foo # fades>2 |
| 132 |
""")) |
| 133 |
assert parsed == { |
| 134 |
REPO_PYPI: get_reqs('foo >2') |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
def test_version_greater_two_spaces(): |
| 139 |
parsed = parsing._parse_content(io.StringIO(""" |
| 140 |
import foo # fades > 2 |
| 141 |
""")) |
| 142 |
assert parsed == { |
| 143 |
REPO_PYPI: get_reqs('foo > 2') |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
def test_version_greater_one_space_after(): |
| 148 |
parsed = parsing._parse_content(io.StringIO(""" |
| 149 |
import foo # fades> 2 |
| 150 |
""")) |
| 151 |
assert parsed == { |
| 152 |
REPO_PYPI: get_reqs('foo > 2') |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
def test_version_greater_two_space_after(): |
| 157 |
parsed = parsing._parse_content(io.StringIO(""" |
| 158 |
import foo # fades> 2 |
| 159 |
""")) |
| 160 |
assert parsed == { |
| 161 |
REPO_PYPI: get_reqs('foo > 2') |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
def test_version_greater_one_space_before(): |
| 166 |
parsed = parsing._parse_content(io.StringIO(""" |
| 167 |
import foo # fades> 2 |
| 168 |
""")) |
| 169 |
assert parsed == { |
| 170 |
REPO_PYPI: get_reqs('foo > 2') |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
def test_version_greater_two_space_before(): |
| 175 |
parsed = parsing._parse_content(io.StringIO(""" |
| 176 |
import foo # fades> 2 |
| 177 |
""")) |
| 178 |
assert parsed == { |
| 179 |
REPO_PYPI: get_reqs('foo > 2') |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
def test_version_same_or_greater(): |
| 184 |
parsed = parsing._parse_content(io.StringIO(""" |
| 185 |
import foo # fades >= 2 |
| 186 |
""")) |
| 187 |
assert parsed == { |
| 188 |
REPO_PYPI: get_reqs('foo >= 2') |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
def test_version_same_or_greater_no_spaces(): |
| 193 |
parsed = parsing._parse_content(io.StringIO(""" |
| 194 |
import foo # fades>=2 |
| 195 |
""")) |
| 196 |
assert parsed == { |
| 197 |
REPO_PYPI: get_reqs('foo >= 2') |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
def test_version_same_or_greater_one_space_before(): |
| 202 |
parsed = parsing._parse_content(io.StringIO(""" |
| 203 |
import foo # fades >=2 |
| 204 |
""")) |
| 205 |
assert parsed == { |
| 206 |
REPO_PYPI: get_reqs('foo >=2') |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
def test_version_same_or_greater_two_space_before(): |
| 211 |
parsed = parsing._parse_content(io.StringIO(""" |
| 212 |
import foo # fades >=2 |
| 213 |
""")) |
| 214 |
assert parsed == { |
| 215 |
REPO_PYPI: get_reqs('foo >=2') |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
def test_version_same_or_greater_one_space_after(): |
| 220 |
parsed = parsing._parse_content(io.StringIO(""" |
| 221 |
import foo # fades>= 2 |
| 222 |
""")) |
| 223 |
assert parsed == { |
| 224 |
REPO_PYPI: get_reqs('foo >= 2') |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
def test_version_same_or_greater_two_space_after(): |
| 229 |
parsed = parsing._parse_content(io.StringIO(""" |
| 230 |
import foo # fades>= 2 |
| 231 |
""")) |
| 232 |
assert parsed == { |
| 233 |
REPO_PYPI: get_reqs('foo >= 2') |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
def test_continuation_line(): |
| 238 |
parsed = parsing._parse_content(io.StringIO(""" |
| 239 |
import bar |
| 240 |
# fades > 2 |
| 241 |
import foo |
| 242 |
""")) |
| 243 |
assert parsed == { |
| 244 |
REPO_PYPI: get_reqs('foo > 2') |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
def test_from_import_simple(): |
| 249 |
parsed = parsing._parse_content(io.StringIO(""" |
| 250 |
from foo import bar # fades |
| 251 |
""")) |
| 252 |
assert parsed == { |
| 253 |
REPO_PYPI: get_reqs('foo') |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
def test_import(): |
| 258 |
parsed = parsing._parse_content(io.StringIO(""" |
| 259 |
import foo.bar # fades |
| 260 |
""")) |
| 261 |
assert parsed == { |
| 262 |
REPO_PYPI: get_reqs('foo') |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
def test_from_import_complex(): |
| 267 |
parsed = parsing._parse_content(io.StringIO(""" |
| 268 |
from baz.foo import bar # fades |
| 269 |
""")) |
| 270 |
assert parsed == { |
| 271 |
REPO_PYPI: get_reqs('baz') |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
def test_allow_other_comments(): |
| 276 |
parsed = parsing._parse_content(io.StringIO(""" |
| 277 |
from foo import * # NOQA # fades |
| 278 |
""")) |
| 279 |
assert parsed == { |
| 280 |
REPO_PYPI: get_reqs('foo') |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
def test_allow_other_comments_reverse_default(): |
| 285 |
parsed = parsing._parse_content(io.StringIO(""" |
| 286 |
from foo import * # fades # NOQA |
| 287 |
""")) |
| 288 |
assert parsed == { |
| 289 |
REPO_PYPI: get_reqs('foo') |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
def test_strange_import(logs): |
| 294 |
parsed = parsing._parse_content(io.StringIO(""" |
| 295 |
from foo bar import :( # fades |
| 296 |
""")) |
| 297 |
assert Exact( |
| 298 |
"Not understood import info: ['from', 'foo', 'bar', 'import', ':(']") in logs.debug |
| 299 |
assert parsed == {} |
| 300 |
|
| 301 |
|
| 302 |
def test_strange_fadesinfo(logs): |
| 303 |
parsed = parsing._parse_content(io.StringIO(""" |
| 304 |
import foo # fades broken::whatever |
| 305 |
""")) |
| 306 |
assert "Not understood fades repository: 'broken'" in logs.warning |
| 307 |
assert parsed == {} |
| 308 |
|
| 309 |
|
| 310 |
def test_strange_fadesinfo2(logs): |
| 311 |
parsed = parsing._parse_content(io.StringIO(""" |
| 312 |
import foo # fadesbroken |
| 313 |
""")) |
| 314 |
assert "Not understood fades info: 'fadesbroken'" in logs.warning |
| 315 |
assert parsed == {} |
| 316 |
|
| 317 |
|
| 318 |
def test_projectname_noversion_implicit(): |
| 319 |
parsed = parsing._parse_content(io.StringIO(""" |
| 320 |
import foo # fades othername |
| 321 |
""")) |
| 322 |
assert parsed == { |
| 323 |
REPO_PYPI: get_reqs('othername') |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
def test_projectname_noversion_explicit(): |
| 328 |
parsed = parsing._parse_content(io.StringIO(""" |
| 329 |
import foo # fades pypi::othername |
| 330 |
""")) |
| 331 |
assert parsed == { |
| 332 |
REPO_PYPI: get_reqs('othername') |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
def test_projectname_version_explicit(): |
| 337 |
parsed = parsing._parse_content(io.StringIO(""" |
| 338 |
import foo # fades pypi::othername >= 3 |
| 339 |
""")) |
| 340 |
assert parsed == { |
| 341 |
REPO_PYPI: get_reqs('othername >= 3') |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
def test_projectname_version_nospace(): |
| 346 |
parsed = parsing._parse_content(io.StringIO(""" |
| 347 |
import foo # fades othername==5 |
| 348 |
""")) |
| 349 |
assert parsed == { |
| 350 |
REPO_PYPI: get_reqs('othername==5') |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
def test_projectname_version_space(): |
| 355 |
parsed = parsing._parse_content(io.StringIO(""" |
| 356 |
import foo # fades othername <5 |
| 357 |
""")) |
| 358 |
assert parsed == { |
| 359 |
REPO_PYPI: get_reqs('othername <5') |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
def test_projectname_pkgnamedb(): |
| 364 |
parsed = parsing._parse_content(io.StringIO(""" |
| 365 |
import bs4 # fades |
| 366 |
""")) |
| 367 |
assert parsed == { |
| 368 |
REPO_PYPI: get_reqs('beautifulsoup4') |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
def test_projectname_pkgnamedb_version(): |
| 373 |
parsed = parsing._parse_content(io.StringIO(""" |
| 374 |
import bs4 # fades >=5 |
| 375 |
""")) |
| 376 |
assert parsed == { |
| 377 |
REPO_PYPI: get_reqs('beautifulsoup4 >=5') |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
def test_projectname_pkgnamedb_othername_default(): |
| 382 |
parsed = parsing._parse_content(io.StringIO(""" |
| 383 |
import bs4 # fades othername |
| 384 |
""")) |
| 385 |
assert parsed == { |
| 386 |
REPO_PYPI: get_reqs('othername') |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
def test_projectname_pkgnamedb_version_othername(): |
| 391 |
parsed = parsing._parse_content(io.StringIO(""" |
| 392 |
import bs4 # fades othername >=5 |
| 393 |
""")) |
| 394 |
assert parsed == { |
| 395 |
REPO_PYPI: get_reqs('othername >=5') |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
def test_comma_separated_import(): |
| 400 |
parsed = parsing._parse_content(io.StringIO(""" |
| 401 |
from foo import bar, baz, qux # fades |
| 402 |
""")) |
| 403 |
assert parsed == { |
| 404 |
REPO_PYPI: get_reqs('foo') |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
def test_other_lines_with_fades_string(): |
| 409 |
parsed = parsing._parse_content(io.StringIO(""" |
| 410 |
import bar # fades |
| 411 |
print("screen fades to black") |
| 412 |
""")) |
| 413 |
assert parsed == { |
| 414 |
REPO_PYPI: get_reqs('bar') |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
def test_commented_line(logs): |
| 419 |
parsed = parsing._parse_content(io.StringIO(""" |
| 420 |
#import foo # fades |
| 421 |
""")) |
| 422 |
assert parsed == {} |
| 423 |
assert "Not understood fades" not in logs.warning |
| 424 |
|
| 425 |
|
| 426 |
def test_with_fades_commented_line(logs): |
| 427 |
parsed = parsing._parse_content(io.StringIO(""" |
| 428 |
#import foo # fades |
| 429 |
import bar # fades |
| 430 |
""")) |
| 431 |
assert parsed == { |
| 432 |
REPO_PYPI: get_reqs('bar') |
| 433 |
} |
| 434 |
assert "Not understood fades" not in logs.warning |
| 435 |
|
| 436 |
|
| 437 |
def test_with_commented_line(logs): |
| 438 |
parsed = parsing._parse_content(io.StringIO(""" |
| 439 |
import bar # fades |
| 440 |
# a commented line |
| 441 |
""")) |
| 442 |
assert parsed == { |
| 443 |
REPO_PYPI: get_reqs('bar') |
| 444 |
} |
| 445 |
assert "Not understood fades" not in logs.warning |
| 446 |
|
| 447 |
|
| 448 |
def test_vcs_explicit(): |
| 449 |
parsed = parsing._parse_content(io.StringIO(""" |
| 450 |
import foo # fades vcs::superurl |
| 451 |
""")) |
| 452 |
assert parsed == { |
| 453 |
REPO_VCS: [parsing.VCSDependency('superurl')] |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
def test_vcs_implicit(): |
| 458 |
parsed = parsing._parse_content(io.StringIO(""" |
| 459 |
import foo # fades http://www.whatever/project |
| 460 |
""")) |
| 461 |
assert parsed == { |
| 462 |
REPO_VCS: [parsing.VCSDependency('http://www.whatever/project')] |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
def test_mixed(): |
| 467 |
parsed = parsing._parse_content(io.StringIO(""" |
| 468 |
import foo # fades vcs::superurl |
| 469 |
import bar # fades |
| 470 |
""")) |
| 471 |
assert parsed == { |
| 472 |
REPO_VCS: [parsing.VCSDependency('superurl')], |
| 473 |
REPO_PYPI: get_reqs('bar'), |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
def test_fades_and_hashtag_mentioned_in_code(): |
| 478 |
"""Test the case where a string contains both: fades and hashtag (#) |
| 479 |
but is not an import. |
| 480 |
""" |
| 481 |
parsed = parsing._parse_content(io.StringIO(""" |
| 482 |
'http://fades.readthedocs.io/en/release-7-0/readme.html#how-to-use-it' |
| 483 |
""")) |
| 484 |
assert parsed == {} |
| 485 |
|
| 486 |
|
| 487 |
def test_fades_and_hashtag_mentioned_in_code_mixed_with_imports(): |
| 488 |
parsed = parsing._parse_content(io.StringIO("""import requests # fades |
| 489 |
|
| 490 |
'http://fades.readthedocs.io/en/release-7-0/readme.html#how-to-use-it' |
| 491 |
""")) |
| 492 |
assert parsed == { |
| 493 |
REPO_PYPI: get_reqs('requests') |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
def test_fades_user_strange_comment_with_hashtag_ignored(): |
| 498 |
parsed = parsing._parse_content(io.StringIO(""" |
| 499 |
import foo # fades==2 # Some comment with #hashtash |
| 500 |
""")) |
| 501 |
assert parsed == {} |