File: test_initialisation.py

package info (click to toggle)
ford 7.0.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,000 kB
  • sloc: python: 11,852; f90: 419; javascript: 51; fortran: 45; makefile: 23
file content (250 lines) | stat: -rw-r--r-- 6,986 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import ford
from textwrap import dedent
from pathlib import Path
import sys
import pytest

import tomli_w

from conftest import gfortran_is_not_installed


class FakeFile:
    name = "test file"


def test_quiet_false(tmp_path):
    _, data = ford.load_settings("quiet: False", tmp_path)
    assert data.quiet is False
    _, data2 = ford.load_settings("quiet: True", tmp_path)
    assert data2.quiet is True


def test_toml(tmp_path):
    settings_file = tmp_path / "fpm.toml"
    settings = {
        "extra": {
            "ford": {
                "quiet": True,
                "display": ["public", "protected"],
                "src_dir": "./source",
            }
        }
    }
    settings_file.write_text(tomli_w.dumps(settings))

    _, data = ford.load_settings("", tmp_path)

    assert data.quiet is True
    assert data.display[0] == "public"
    assert data.display[1] == "protected"
    assert data.src_dir == ["./source"]


def test_quiet_command_line():
    """Check that setting --quiet on the command line overrides project file"""

    data, _ = ford.parse_arguments(
        {"quiet": True, "preprocess": False}, "", ford.ProjectSettings(quiet=False)
    )
    assert data.quiet is True
    data, _ = ford.parse_arguments(
        {"quiet": False, "preprocess": False}, "", (ford.ProjectSettings(quiet=True))
    )
    assert data.quiet is False


def test_list_input(tmp_path):
    """Check that setting a non-list option is turned into a single string"""

    settings = """\
    include: line1
             line2
    summary: This
             is
             one
             string
    """
    _, data = ford.load_settings(dedent(settings), tmp_path)

    assert len(data.include) == 2
    assert data.summary == "This\nis\none\nstring"


def test_path_normalisation():
    """Check that paths get normalised correctly"""

    data, _ = ford.parse_arguments(
        {"preprocess": False},
        "",
        ford.ProjectSettings(page_dir="my_pages", src_dir=["src1", "src2"]),
        directory="/prefix/path",
    )
    assert data.page_dir == Path("/prefix/path/my_pages").absolute()
    assert data.src_dir == [
        Path("/prefix/path/src1").absolute(),
        Path("/prefix/path/src2").absolute(),
    ]


def test_source_not_subdir_output():
    """Check if the src_dir is correctly detected as being a subdirectory of output_dir"""

    # This should be fine
    data, _ = ford.parse_arguments(
        {"src_dir": ["/1/2/3", "4/5"], "output_dir": "/3/4", "preprocess": False},
        "",
        ford.ProjectSettings(),
        directory="/prefix",
    )

    # This shouldn't be
    with pytest.raises(ValueError):
        data, _ = ford.parse_arguments(
            {"src_dir": ["4/5", "/1/2/3"], "output_dir": "/1/2", "preprocess": False},
            "",
            ford.ProjectSettings(),
            directory="/prefix",
        )
    # src_dir == output_dir
    with pytest.raises(ValueError):
        data, _ = ford.parse_arguments(
            {"src_dir": ["/1/2/"], "output_dir": "/1/2", "preprocess": False},
            "",
            ford.ProjectSettings(),
            directory="/prefix",
        )


def test_repeated_docmark():
    """Check that setting --quiet on the command line overrides project file"""

    with pytest.raises(ValueError):
        ford.parse_arguments(
            {"preprocess": False},
            "",
            (ford.ProjectSettings(**{"docmark": "!", "predocmark": "!"})),
        )

    with pytest.raises(ValueError):
        ford.parse_arguments(
            {"preprocess": False},
            "",
            (ford.ProjectSettings(**{"docmark": "!<", "predocmark_alt": "!<"})),
        )

    with pytest.raises(ValueError):
        ford.parse_arguments(
            {"preprocess": False},
            "",
            (ford.ProjectSettings(**{"docmark_alt": "!!", "predocmark_alt": "!!"})),
        )


def test_no_preprocessor():
    data, _ = ford.parse_arguments(
        {}, "", (ford.ProjectSettings(**{"preprocess": False}))
    )

    assert data.fpp_extensions == []


def test_bad_preprocessor():
    with pytest.raises(SystemExit):
        ford.parse_arguments(
            {"project_file": FakeFile()},
            "",
            (ford.ProjectSettings(**{"preprocessor": "false"})),
        )


@pytest.mark.skipif(
    sys.platform.startswith("win"), reason="FIXME: Need portable do-nothing command"
)
def test_maybe_ok_preprocessor():
    data, _ = ford.parse_arguments(
        {}, "", (ford.ProjectSettings(**{"preprocessor": "true"}))
    )

    if data.preprocess is True:
        assert data.preprocessor == "true"


@pytest.mark.skipif(
    gfortran_is_not_installed(), reason="Requires gfortran to be installed"
)
def test_gfortran_preprocessor():
    data, _ = ford.parse_arguments(
        {}, "", (ford.ProjectSettings(**{"preprocessor": "gfortran -E"}))
    )

    assert data.preprocess is True


def test_absolute_src_dir(monkeypatch, tmp_path):
    project_file = tmp_path / "example.md"
    project_file.write_text("preprocess: False")
    src_dir = tmp_path / "not_here"

    with monkeypatch.context() as m:
        m.setattr(sys, "argv", ["ford", str(project_file)])
        args, _ = ford.initialize()

    assert args.src_dir == [tmp_path / "./src"]

    with monkeypatch.context() as m:
        m.setattr(sys, "argv", ["ford", str(project_file), "--src_dir", str(src_dir)])
        args, _ = ford.initialize()

    assert args.src_dir == [src_dir]

    with monkeypatch.context() as m:
        m.setattr(
            sys, "argv", ["ford", "--src_dir", str(src_dir), "--", str(project_file)]
        )
        args, _ = ford.initialize()

    assert args.src_dir == [src_dir]


def test_output_dir_cli(monkeypatch, tmp_path):
    project_file = tmp_path / "example.md"
    project_file.write_text("preprocess: False")

    with monkeypatch.context() as m:
        m.setattr(sys, "argv", ["ford", str(project_file), "--output_dir", "something"])
        settings, _ = ford.initialize()

    assert settings.output_dir == tmp_path / "something"

    with open(project_file, "a") as f:
        f.write("\noutput_dir: something_else")

    with monkeypatch.context() as m:
        m.setattr(sys, "argv", ["ford", str(project_file)])
        settings, _ = ford.initialize()

    assert settings.output_dir == tmp_path / "something_else"


def test_config_option():
    command_line_args = {
        "preprocess": False,
        "config": "quiet = true; display = ['public']; alias = {a = 'b', c = 'd'}",
    }

    data, _ = ford.parse_arguments(
        command_line_args, "", ford.ProjectSettings(quiet=False)
    )

    assert data.quiet is True
    assert data.display == ["public"]
    assert data.alias == {"a": "b", "c": "d"}


def test_external_links_command_line_arg():
    data, _ = ford.parse_arguments(
        {"external": "remote = https://some.link"}, "", ford.ProjectSettings()
    )

    assert data.external == {"remote": "https://some.link"}