|
| 1 | +# |
| 2 | +# sambacc: a samba container configuration tool |
| 3 | +# Copyright (C) 2024 John Mulligan |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/> |
| 17 | +# |
| 18 | + |
| 19 | +from unittest import mock |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from sambacc.commands import skips |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize( |
| 27 | + "value,rtype", |
| 28 | + [ |
| 29 | + ("always:", skips.SkipAlways), |
| 30 | + ("file:/var/lib/womble", skips.SkipFile), |
| 31 | + ("file:!/var/lib/zomble", skips.SkipFile), |
| 32 | + ("env:LIMIT==none", skips.SkipEnv), |
| 33 | + ("env:LIMIT!=everybody", skips.SkipEnv), |
| 34 | + ("env:LIMIT=everybody", ValueError), |
| 35 | + ("env:LIMIT", ValueError), |
| 36 | + ("file:", ValueError), |
| 37 | + ("always:forever", ValueError), |
| 38 | + ("klunk:", KeyError), |
| 39 | + ], |
| 40 | +) |
| 41 | +def test_parse(value, rtype): |
| 42 | + if issubclass(rtype, BaseException): |
| 43 | + with pytest.raises(rtype): |
| 44 | + skips.parse(value) |
| 45 | + return |
| 46 | + skf = skips.parse(value) |
| 47 | + assert isinstance(skf, rtype) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + "value,ret", |
| 52 | + [ |
| 53 | + ("file:/var/lib/foo/a", "skip-if-file-exists: /var/lib/foo/a exists"), |
| 54 | + ( |
| 55 | + "file:!/var/lib/bar/a", |
| 56 | + "skip-if-file-missing: /var/lib/bar/a missing", |
| 57 | + ), |
| 58 | + ("file:/etc/blat", None), |
| 59 | + ("env:PLINK==0", "env var: PLINK -> 0 == 0"), |
| 60 | + ("env:PLINK!=88", "env var: PLINK -> 0 != 88"), |
| 61 | + ("env:PLONK==enabled", None), |
| 62 | + ("always:", "always skip"), |
| 63 | + ], |
| 64 | +) |
| 65 | +def test_method_test(value, ret, monkeypatch): |
| 66 | + def _exists(p): |
| 67 | + rv = p.startswith("/var/lib/foo/") |
| 68 | + return rv |
| 69 | + |
| 70 | + monkeypatch.setattr("os.path.exists", _exists) |
| 71 | + monkeypatch.setenv("PLINK", "0") |
| 72 | + monkeypatch.setenv("PLONK", "disabled") |
| 73 | + skf = skips.parse(value) |
| 74 | + ctx = mock.MagicMock() |
| 75 | + assert skf.test(ctx) == ret |
| 76 | + |
| 77 | + |
| 78 | +def test_test(monkeypatch): |
| 79 | + def _exists(p): |
| 80 | + rv = p.startswith("/var/lib/foo/") |
| 81 | + return rv |
| 82 | + |
| 83 | + monkeypatch.setattr("os.path.exists", _exists) |
| 84 | + monkeypatch.setenv("PLINK", "0") |
| 85 | + monkeypatch.setenv("PLONK", "disabled") |
| 86 | + |
| 87 | + conds = [ |
| 88 | + skips.SkipEnv("==", "PLINK", "1"), |
| 89 | + skips.SkipEnv("!=", "PLONK", "disabled"), |
| 90 | + skips.SkipAlways(), |
| 91 | + ] |
| 92 | + ctx = mock.MagicMock() |
| 93 | + assert skips.test(ctx, conditions=conds) == "always skip" |
| 94 | + conds = conds[:-1] |
| 95 | + assert not skips.test(ctx, conditions=conds) |
| 96 | + monkeypatch.setenv("PLINK", "1") |
| 97 | + assert skips.test(ctx, conditions=conds) == "env var: PLINK -> 1 == 1" |
| 98 | + |
| 99 | + ctx.cli.skip_conditions = conds |
| 100 | + assert skips.test(ctx) == "env var: PLINK -> 1 == 1" |
| 101 | + |
| 102 | + |
| 103 | +def test_help_info(): |
| 104 | + txt = skips._help_info() |
| 105 | + assert "file:" in txt |
| 106 | + assert "env:" in txt |
| 107 | + assert "always:" in txt |
| 108 | + |
| 109 | + |
| 110 | +def test_parse_hack(): |
| 111 | + import argparse |
| 112 | + |
| 113 | + with pytest.raises(argparse.ArgumentTypeError): |
| 114 | + skips.parse("?") |
0 commit comments