File: test_cli_convert.py

package info (click to toggle)
python-canmatrix 1.2~github-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,012 kB
  • sloc: xml: 30,201; python: 14,631; makefile: 31; sh: 7
file content (337 lines) | stat: -rw-r--r-- 12,802 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# -*- coding: utf-8 -*-
import os
import sys
import tempfile

import pytest
import canmatrix.formats

try:
    from pathlib import Path
except ImportError:
    from pathlib2 import Path

pytest_plugins = ["pytester"]
here = Path(__file__).parent / "files"
tmp_dir = tempfile.mkdtemp()


@pytest.fixture
def run(testdir):
    def do_run(*args):
        args = [sys.executable, "-m", "canmatrix.cli.convert"] + list(args)
        return testdir.run(*args)
    return do_run

def test_silent(tmpdir, run):
    inputFile = str(here / "dbc" / "test_frame_decoding.dbc")

    normal_result = run(inputFile, "tmp.dbc")
    silent_result = run("-s", inputFile, "tmp.dbc")
    assert len(normal_result.errlines) > len(silent_result.errlines)

def test_verbose(tmpdir, run):
    inputFile = str(here / "arxml" / "ARXML_min_max.arxml")

    normal_result = run(inputFile, "tmp.dbc")
    verbose_result = run("-vv", inputFile, "tmp.dbc")
    assert len(normal_result.errlines) + len(normal_result.outlines) < len(verbose_result.errlines) + len(verbose_result.outlines)

def test_force_output_format(tmpdir, run):
    inputFile = str(here / "dbc" / "test_frame_decoding.dbc")
    outFile = tmp_dir + "/cli_convert_test_force.tmp"

    normal_result = run("-vv", "-f", "dbc", inputFile, outFile)
    assert 'INFO - convert - Export Done' in normal_result.errlines[-1]

    with open(outFile, "r") as fd:
        first_line = fd.readline()
        assert first_line == 'VERSION "created by canmatrix"\n'

def test_force_input_format(tmpdir, run):
    inputFile = tmp_dir + "/cli_convert_test_force.tmp"

    normal_result = run("-i", "dbc", inputFile, "tmp.dbc")
    assert 'INFO - convert - Export Done' in normal_result.errlines[-1]

def create_dbc_with_special_char():
    outFile = tmp_dir + "/output_cli_convert_tmp.dbc"

    myFrame = canmatrix.Frame("testFrame1", 
                              arbitration_id=canmatrix.arbitration_id_converter(0x123), 
                              size=8, 
                              transmitters=["testBU"])
    mySignal = canmatrix.Signal("someTestSignal",
                      size=11,
                      is_little_endian=False,
                      is_signed=False,
                      factor=5.0,
                      offset=1.0,
                      min=0,
                      max=500,
                      unit=u"specialCharUnit°$",  # .decode("utf-8"),
                      receivers=["recBU"])
    myFrame.add_signal(mySignal)

    db = canmatrix.CanMatrix()
    db.add_frame(myFrame)
    db.add_frame_defines("SomeUnneededDefine", 'INT 0 65535')
    canmatrix.formats.dumpp({"": db},
                            outFile, 
                            dbcExportEncoding='iso-8859-1',
                            dbcExportCommentEncoding='iso-8859-1')
    return outFile

def test_ignore_encoding_errors(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    normal_result = run("--ignoreEncodingErrors", "--dbcExportEncoding", "ascii", inputFile, "tmp2.dbc")
    assert 'INFO - convert - Export Done' in normal_result.errlines[-1]

def test_delete_obsolete_defines(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    deleted_result = run("--deleteObsoleteDefines", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"SomeUnneededDefine" not in content
    normal_result = run(inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"SomeUnneededDefine" in content

def test_delete_ecu(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    deleted_result = run("--deleteEcu", "testBU", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"testBU" not in content

def test_rename_ecu(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    deleted_result = run("--renameEcu", "testBU:renamedECU", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"testBU" not in content
        assert b"renamedECU" in content

def test_delete_signal(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    deleted_result = run("--deleteSignal", "someTestSignal", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"someTestSignal" not in content

def test_rename_signal(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    deleted_result = run("--renameSignal", "someTestSignal:renamedSignal", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"someTestSignal" not in content
        assert b"renamedSignal" in content

def test_delete_frame(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    deleted_result = run("--deleteFrame", "testFrame1", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "r") as fd:
        content = fd.read()
        assert "testFrame1" not in content

def test_rename_frame(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    deleted_result = run("--renameFrame", "testFrame1:renamedFrame", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"testFrame1" not in content
        assert b"renamedFrame" in content

def test_add_frame_receiver(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    deleted_result = run("--addFrameReceiver", "testFrame1:newECU", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"recBU,newECU" in content

def test_change_frame_id(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    deleted_result = run("--changeFrameId", "291:666", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"BO_ 666" in content

def test_set_frame_fd(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    deleted_result = run("--setFrameFd", "testFrame1", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b'BA_ "VFrameFormat" BO_ 291 14' in content
    deleted_result = run("--unsetFrameFd", "testFrame1", "tmp2.dbc", "tmp3.dbc")
    with open("tmp3.dbc", "rb") as fd:
        content = fd.read()
        assert b'BA_ "VFrameFormat" BO_ 291 14' not in content

def test_recalc_dlc(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    result = run("--recalcDLC", "max", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"testFrame1: 8" in content

    result = run("--recalcDLC", "force", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"testFrame1: 2" in content

def test_skip_long_dlc(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    result = run("--skipLongDlc", "7", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "r") as fd:
        content = fd.read()
        assert "someTestSignal" not in content

def test_cut_long_frames(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    result = run("--cutLongFrames", "1", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "r") as fd:
        content = fd.read()
        assert "someTestSignal" not in content
    result = run("--cutLongFrames", "2", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"someTestSignal" in content

def test_copy_signals(tmpdir, run):
    inputFile = create_dbc_with_special_char()
    result = run("--signals", "someTestSignal", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"someTestSignal" in content
        assert b"VECTOR__INDEPENDENT_SIG_MSG" in content

def create_dbc(additionalReceiver = []):
    tmp_dir = tempfile.mkdtemp()
    outFile = tmp_dir + "/output_cli_convert_tmpb.dbc"

    myFrame = canmatrix.Frame("testFrame3", arbitration_id=canmatrix.arbitration_id_converter(0x124), size=8, transmitters=["testBU"])
    mySignal = canmatrix.Signal("someTestSignal",
                      size=11,
                      is_little_endian=False,
                      is_signed=False,
                      factor=5.0,
                      offset=1.0,
                      min=0,
                      max=500,
                      receivers=["recBU"])
    myFrame.add_signal(mySignal)
    myFrame2 = canmatrix.Frame("testFrame2", arbitration_id=canmatrix.arbitration_id_converter(0x125), size=8, transmitters=["testBU2"])
    myFrame2.add_attribute("myAttribute", "42")
    mySignal2 = canmatrix.Signal("someTestSignal2",
                      start_bit=15,
                      size=11,
                      is_little_endian=False,
                      is_signed=False,
                      factor=5.0,
                      offset=1.0,
                      min=0,
                      max=500,
                      receivers=["recBU2"] + additionalReceiver)
    myFrame2.add_signal(mySignal2)
    mySignal3 = canmatrix.Signal("zeroSignal",
                      start_bit=20,
                      size=0,
                      is_little_endian=False,
                      is_signed=False,
                      factor=5.0,
                      offset=1.0,
                      min=0,
                      max=500,
                      receivers=["recBU2"])
    mySignal3.add_attribute("mySignalAttribute", "7")
    myFrame2.add_signal(mySignal3)

    db = canmatrix.CanMatrix()
    db.add_frame(myFrame)
    db.add_frame(myFrame2)
    db.add_frame_defines("myAttribute", "INT -5 10")
    db.add_signal_defines("mySignalAttribute", 'INT 0 65535')
    canmatrix.formats.dumpp({"": db}, outFile, dbcExportEncoding='iso-8859-1',
                            dbcExportCommentEncoding='iso-8859-1')
    return outFile

def test_copy_ecus(tmpdir, run):
    inputFile = create_dbc()
    result = run("--ecus", "testBU", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "r") as fd:
        content = fd.read()
        assert "testBU2" not in content
        assert "testBU" in content

def test_copy_ecus_rx(tmpdir, run):
    inputFile = create_dbc()
    result = run("--ecus", "recBU:rx", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "r") as fd:
        content = fd.read()
        assert "recBU2" not in content
        assert "recBU" in content

def test_copy_ecus_tx(tmpdir, run):
    inputFile = create_dbc(additionalReceiver = ["testBU"])
    result = run("--ecus", "testBU:tx", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "r") as fd:
        content = fd.read()
        assert "testFrame2" not in content
        assert "testFrame3" in content

def test_copy_frames(tmpdir, run):
    inputFile = create_dbc()
    result = run("--frames", "testFrame3", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"testFrame2" not in content
        assert b"testFrame3" in content

def test_delete_frame_attributes(tmpdir, run):
    inputFile = create_dbc()
    result = run("--deleteFrameAttributes", "myAttribute", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "r") as fd:
        content = fd.read()
        assert 'BA_ "myAttribute"' not in content

def test_delete_zero_signals(tmpdir, run):
    inputFile = create_dbc()
    result = run("--deleteZeroSignals", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "r") as fd:
        content = fd.read()
        assert 'zeroSignal' not in content

def test_delete_signal_attributes(tmpdir, run):
    inputFile = create_dbc()
    result = run("--deleteSignalAttributes", "mySignalAttribute", inputFile, "tmp2.dbc")
    with open("tmp2.dbc", "r") as fd:
        content = fd.read()
        assert 'BA_ "mySignalAttribute"' not in content

def test_merge(tmpdir, run):
    inputFile1 = create_dbc_with_special_char()
    inputFile2 = create_dbc()

    if inputFile1[1] == ":":
        inputFile1 = inputFile1[2:]
    result = run("--merge", inputFile1, inputFile2, "tmp2.dbc")
    with open("tmp2.dbc", "rb") as fd:
        content = fd.read()
        assert b"BO_ 291" in content
        assert b"BO_ 292" in content
        assert b"BO_ 293" in content

    result = run("--merge", inputFile1 + ":ecu=testBU", inputFile2, "tmp3.dbc")
    with open("tmp3.dbc", "rb") as fd:
        content = fd.read()
        assert b"BO_ 291" in content
        assert b"BO_ 292" in content
        assert b"BO_ 293" in content

    result = run("--merge", inputFile1 + ":frame=testFrame1", inputFile2, "tmp4.dbc")
    with open("tmp4.dbc", "rb") as fd:
        content = fd.read()
        assert b"BO_ 291" in content
        assert b"BO_ 292" in content
        assert b"BO_ 293" in content