blob: 92ca4a5104db3e42e3281f280951e4029497a3f1 [file] [log] [blame]
Claudiu Popaec09cc72016-07-23 00:22:28 +03001# Copyright (c) 2006-2008, 2010, 2013 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
Claudiu Popa9ff44de2017-12-15 12:24:15 +01002# Copyright (c) 2012 FELD Boris <lothiraldan@gmail.com>
hippo911f7c29c2020-08-20 18:40:19 +02003# Copyright (c) 2014-2018, 2020 Claudiu Popa <pcmanticore@gmail.com>
Claudiu Popa9ff44de2017-12-15 12:24:15 +01004# Copyright (c) 2014 Google, Inc.
5# Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
6# Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
Claudiu Popa3a8635a2018-07-15 11:36:36 +02007# Copyright (c) 2018 Reverb C <reverbc@users.noreply.github.com>
Pierre Sassoulasac852232021-02-21 15:13:06 +01008# Copyright (c) 2019-2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
Claudiu Popa369d9522020-04-27 11:04:14 +02009# Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk>
Pierre Sassoulasac852232021-02-21 15:13:06 +010010# Copyright (c) 2020 hippo91 <guillaume.peillex@gmail.com>
hippo911f7c29c2020-08-20 18:40:19 +020011# Copyright (c) 2020 Damien Baty <damien.baty@polyconseil.fr>
Claudiu Popa369d9522020-04-27 11:04:14 +020012# Copyright (c) 2020 Frank Harrison <frank@doublethefish.com>
Pierre Sassoulas0f08e2e2021-06-17 10:21:08 +020013# Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Pierre Sassoulas13dae7c2021-03-29 20:59:59 +020014# Copyright (c) 2021 Andrew Howe <howeaj@users.noreply.github.com>
Claudiu Popaec09cc72016-07-23 00:22:28 +030015
Claudiu Popa83227812016-06-01 16:11:29 +010016# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
Marc Mueller21290862021-07-01 12:47:58 +020017# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
Pierre Sassoulasec2957f2020-05-01 13:53:10 +020018# pylint: disable=redefined-outer-name
Claudiu Popa83227812016-06-01 16:11:29 +010019
root4becf6f2006-04-26 10:48:09 +000020import os
Pierre Sassoulas5f7ce972021-03-30 23:02:16 +020021import shutil
root4becf6f2006-04-26 10:48:09 +000022from os.path import exists
root4becf6f2006-04-26 10:48:09 +000023
Derek Gustafson197555a2016-12-08 04:15:21 +000024import pytest
Torsten Marek0ed61a72014-11-09 10:33:29 +010025
Damien Baty89f1a6f2020-07-06 00:01:50 +020026from pylint import testutils
Pierre Sassoulas5ab140a2019-03-09 11:22:36 +010027from pylint.checkers import imports, initialize
28from pylint.lint import PyLinter
root4becf6f2006-04-26 10:48:09 +000029
Derek Gustafson4faacf22016-12-06 15:42:53 +000030
Derek Gustafson197555a2016-12-08 04:15:21 +000031@pytest.fixture
Andrew Howea37c6432021-03-29 09:00:18 +010032def dest(request):
33 dest = request.param
Derek Gustafson197555a2016-12-08 04:15:21 +000034 yield dest
Frank Harrison23c4ae82020-04-07 17:52:16 +010035 try:
36 os.remove(dest)
37 except FileNotFoundError:
38 # file may not have been created if tests inside fixture skipped
39 pass
Derek Gustafson4faacf22016-12-06 15:42:53 +000040
Sylvain Thénaultede82d72010-04-19 11:22:40 +020041
Andrew Howea37c6432021-03-29 09:00:18 +010042POSSIBLE_DOT_FILENAMES = ["foo.dot", "foo.gv", "tests/regrtest_data/foo.dot"]
43
44
45@pytest.mark.parametrize("dest", POSSIBLE_DOT_FILENAMES, indirect=True)
Derek Gustafson197555a2016-12-08 04:15:21 +000046def test_dependencies_graph(dest):
Andrew Howea37c6432021-03-29 09:00:18 +010047 """DOC files are correctly generated, and the graphname is the basename"""
Claudiu Popa3f284242018-09-16 17:33:50 +020048 imports._dependencies_graph(dest, {"labas": ["hoho", "yep"], "hoho": ["yep"]})
Derek Gustafson197555a2016-12-08 04:15:21 +000049 with open(dest) as stream:
Claudiu Popa3f284242018-09-16 17:33:50 +020050 assert (
51 stream.read().strip()
52 == """
Andrew Howea37c6432021-03-29 09:00:18 +010053digraph "foo" {
Emile Anclinbe37bb32008-09-10 10:19:26 +020054rankdir=LR
55charset="utf-8"
Emile Anclin176bbe52008-09-15 12:21:13 +020056URL="." node[shape="box"]
Emile Anclinbe37bb32008-09-10 10:19:26 +020057"hoho" [];
58"yep" [];
59"labas" [];
Sylvain Thénaultbd7048c2008-12-16 17:15:59 +010060"yep" -> "hoho" [];
61"hoho" -> "labas" [];
62"yep" -> "labas" [];
root4becf6f2006-04-26 10:48:09 +000063}
Claudiu Popa3f284242018-09-16 17:33:50 +020064""".strip()
65 )
Sylvain Thénaultede82d72010-04-19 11:22:40 +020066
Derek Gustafson4faacf22016-12-06 15:42:53 +000067
Andrew Howea37c6432021-03-29 09:00:18 +010068@pytest.mark.parametrize("filename", ["graph.png", "graph"])
Pierre Sassoulas5f7ce972021-03-30 23:02:16 +020069@pytest.mark.skipif(
Marc Mueller5cd19d22021-06-01 20:54:10 +020070 any(shutil.which(x) for x in ("dot", "gv")), reason="dot or gv is installed"
Pierre Sassoulas5f7ce972021-03-30 23:02:16 +020071)
Andrew Howea37c6432021-03-29 09:00:18 +010072def test_missing_graphviz(filename):
73 """Raises if graphviz is not installed, and defaults to png if no extension given"""
74 with pytest.raises(RuntimeError, match=r"Cannot generate `graph\.png`.*"):
75 imports._dependencies_graph(filename, {"a": ["b", "c"], "b": ["c"]})
76
77
Derek Gustafson197555a2016-12-08 04:15:21 +000078@pytest.fixture
79def linter():
Pierre Sassoulas3aa50452021-02-07 14:15:05 +010080 pylinter = PyLinter(reporter=testutils.GenericTestReporter())
81 initialize(pylinter)
82 return pylinter
Sylvain Thénaultede82d72010-04-19 11:22:40 +020083
Derek Gustafson197555a2016-12-08 04:15:21 +000084
85@pytest.fixture
86def remove_files():
87 yield
Claudiu Popa3f284242018-09-16 17:33:50 +020088 for fname in ("import.dot", "ext_import.dot", "int_import.dot"):
root4becf6f2006-04-26 10:48:09 +000089 try:
Derek Gustafson197555a2016-12-08 04:15:21 +000090 os.remove(fname)
Pierre Sassoulas5613da52020-04-20 16:38:10 +020091 except FileNotFoundError:
Derek Gustafson197555a2016-12-08 04:15:21 +000092 pass
93
94
Reverb C5cbb8c92018-03-01 16:55:05 +080095@pytest.mark.usefixtures("remove_files")
Derek Gustafson197555a2016-12-08 04:15:21 +000096def test_checker_dep_graphs(linter):
Pierre Sassoulas3aa50452021-02-07 14:15:05 +010097 linter.global_set_option("persistent", False)
98 linter.global_set_option("reports", True)
99 linter.global_set_option("enable", "imports")
100 linter.global_set_option("import-graph", "import.dot")
101 linter.global_set_option("ext-import-graph", "ext_import.dot")
102 linter.global_set_option("int-import-graph", "int_import.dot")
103 linter.global_set_option("int-import-graph", "int_import.dot")
Derek Gustafson197555a2016-12-08 04:15:21 +0000104 # ignore this file causing spurious MemoryError w/ some python version (>=2.3?)
Pierre Sassoulas3aa50452021-02-07 14:15:05 +0100105 linter.global_set_option("ignore", ("func_unknown_encoding.py",))
106 linter.check("input")
107 linter.generate_reports()
Claudiu Popa3f284242018-09-16 17:33:50 +0200108 assert exists("import.dot")
109 assert exists("ext_import.dot")
110 assert exists("int_import.dot")