1
|
|
|
# pylint: disable=redefined-outer-name,unused-argument,unused-variable,expression-not-assigned,singleton-comparison |
2
|
|
|
|
3
|
|
|
import os |
4
|
|
|
from pathlib import Path |
5
|
|
|
from contextlib import suppress |
6
|
|
|
|
7
|
|
|
import pytest |
8
|
|
|
from expecter import expect |
9
|
|
|
|
10
|
|
|
from click.testing import CliRunner |
11
|
|
|
|
12
|
|
|
from envdiff.cli import main, do_run |
13
|
|
|
from envdiff.models import Config, SourceFile, Environment |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
@pytest.fixture |
17
|
|
|
def runner(): |
18
|
|
|
return CliRunner() |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
@pytest.yield_fixture |
22
|
|
|
def tmp(path=Path("tmp", "int", "cli").resolve()): |
23
|
|
|
cwd = Path.cwd() |
24
|
|
|
path.mkdir(parents=True, exist_ok=True) |
25
|
|
|
os.chdir(path) |
26
|
|
|
yield path |
27
|
|
|
os.chdir(cwd) |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def describe_cli(): |
31
|
|
|
|
32
|
|
|
def describe_init(): |
33
|
|
|
|
34
|
|
|
def when_config_missing(runner, tmp): |
35
|
|
|
path = tmp.joinpath("env-diff.yml") |
36
|
|
|
with suppress(FileNotFoundError): |
37
|
|
|
path.unlink() |
38
|
|
|
|
39
|
|
|
result = runner.invoke(main, ['--init']) |
40
|
|
|
|
41
|
|
|
expect(result.output) == ( |
42
|
|
|
"Generated config file: {}\n".format(path) + |
43
|
|
|
"Edit this file to match your application\n" |
44
|
|
|
) |
45
|
|
|
expect(result.exit_code) == 0 |
46
|
|
|
|
47
|
|
|
def describe_run(): |
48
|
|
|
|
49
|
|
|
def when_config_missing(runner, tmp): |
50
|
|
|
path = tmp.joinpath("env-diff.yml") |
51
|
|
|
with suppress(FileNotFoundError): |
52
|
|
|
path.unlink() |
53
|
|
|
|
54
|
|
|
result = runner.invoke(main, []) |
55
|
|
|
|
56
|
|
|
expect(result.output) == ( |
57
|
|
|
"No config file found\n" + |
58
|
|
|
"Generate one with the '--init' command\n" |
59
|
|
|
) |
60
|
|
|
expect(result.exit_code) == 1 |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def describe_do_run(): |
64
|
|
|
|
65
|
|
|
@pytest.fixture |
66
|
|
|
def config(tmpdir): |
67
|
|
|
tmpdir.chdir() |
68
|
|
|
|
69
|
|
|
with Path(".env").open('w') as f: |
70
|
|
|
f.write("FOO=1") |
71
|
|
|
|
72
|
|
|
with Path("app.py").open('w') as f: |
73
|
|
|
f.write("os.getenv('FOO', 2)") |
74
|
|
|
|
75
|
|
|
return Config.new( |
76
|
|
|
sourcefiles=[ |
77
|
|
|
SourceFile(".env"), |
78
|
|
|
SourceFile("app.py"), |
79
|
|
|
], |
80
|
|
|
environments=[ |
81
|
|
|
Environment("test", command="echo FOO=3"), |
82
|
|
|
], |
83
|
|
|
) |
84
|
|
|
|
85
|
|
|
def it_returns_table_data(runner, config): |
86
|
|
|
print(config.sourcefiles) |
87
|
|
|
data = do_run(config) |
88
|
|
|
|
89
|
|
|
expect(list(data)) == [ |
90
|
|
|
['Variable', 'File: .env', 'File: app.py', 'Environment: test'], |
91
|
|
|
['FOO', 'FOO=1', "os.getenv('FOO', 2)", '3'], |
92
|
|
|
] |
93
|
|
|
|