1""" Test str processors on actual file contents """
2import os
3
4import config
5from flynt.process import fstringify_code_by_line
6
7int_test_dir = os.path.join(config.home, "test/integration/")
8
9in_dir = os.path.join(int_test_dir, "samples_in")
10out_dir = os.path.join(int_test_dir, "actual_out_single_line")
11expected_dir = os.path.join(int_test_dir, "expected_out_single_line")
12
13os.makedirs(out_dir, exist_ok=True)
14
15
16def read_in(name):
17    filepath = os.path.join(in_dir, name)
18    with open(filepath) as f:
19        txt = f.read()
20
21    return txt
22
23
24def read_expected(name):
25    filepath = os.path.join(expected_dir, name)
26    with open(filepath) as f:
27        txt = f.read()
28
29    return txt
30
31
32def write_output_file(name, txt):
33    filepath = os.path.join(out_dir, name)
34    with open(filepath, "w") as f:
35        f.write(txt)
36
37
38def try_on_file(filename: str, multiline):
39    """ Given a file name (something.py) find this file in test/integration/samples_in,
40    run flint_str on its content, write result to
41    test/integration/actual_out/something.py,
42    and compare the result with test/integration/expected_out/something.py"""
43    txt_in = read_in(filename)
44    out, edits = fstringify_code_by_line(txt_in, multiline=multiline, len_limit=None)
45
46    write_output_file(filename, out)
47    return out, read_expected(filename)
48
49
50def test_fstringify_single_line(filename):
51    out, expected = try_on_file(filename, multiline=False)
52    assert out == expected
53