diff --git a/kiplot/out_bom.py b/kiplot/out_bom.py index 0e13f3c5..a9d2fa0d 100644 --- a/kiplot/out_bom.py +++ b/kiplot/out_bom.py @@ -130,7 +130,7 @@ class BoMCSV(Optionable): super().__init__() with document: self.separator = ',' - """ CSV Separator """ + """ CSV Separator. TXT and TSV always use tab as delimiter """ self.hide_pcb_info = False """ Hide project information """ self.hide_stats_info = False diff --git a/tests/test_plot/test_int_bom.py b/tests/test_plot/test_int_bom.py index a374c98b..5c6b8a89 100644 --- a/tests/test_plot/test_int_bom.py +++ b/tests/test_plot/test_int_bom.py @@ -33,7 +33,6 @@ Tests of Internal BoM files Missing: - Variants - number_boards -- various boards - XLSX colors (for real) @@ -186,18 +185,47 @@ def check_head_html(r, info, stats, title, logo): logging.debug('No stats block Ok') -def test_int_bom_simple_csv(): - prj = 'kibom-test' - ext = 'csv' - ctx = context.TestContextSCH('test_int_bom_simple_csv', prj, 'int_bom_simple_csv', BOM_DIR) - ctx.run() - out = prj + '-bom.' + ext - rows, header = ctx.load_csv(out) - assert header == KIBOM_TEST_HEAD +def kibom_verif(rows, header, skip_head=False, qty_name=QTY_COLUMN_NAME): + if not skip_head: + assert header == KIBOM_TEST_HEAD ref_column = header.index(REF_COLUMN_NAME) - qty_column = header.index(QTY_COLUMN_NAME) + qty_column = header.index(qty_name) check_kibom_test_netlist(rows, ref_column, KIBOM_TEST_GROUPS, KIBOM_TEST_EXCLUDE, KIBOM_TEST_COMPONENTS) check_dnc(rows, 'R7', ref_column, qty_column) + + +def kibom_setup(test, ext='csv'): + prj = 'kibom-test' + ctx = context.TestContextSCH('test_'+test, prj, test, BOM_DIR) + ctx.run() + out = prj + '-bom.' + ext + return ctx, out + + +def test_int_bom_simple_csv(): + ctx, out = kibom_setup('int_bom_simple_csv') + rows, header = ctx.load_csv(out) + kibom_verif(rows, header) + # Check not quoted and comma as delimiter + ctx.search_in_file(os.path.join(BOM_DIR, out), [KIBOM_TEST_HEAD[0]+','+KIBOM_TEST_HEAD[1]]) + ctx.clean_up() + + +def test_int_bom_refuse_no_sep(): + ctx, out = kibom_setup('int_bom_refuse_no_sep') + rows, header = ctx.load_csv(out) + kibom_verif(rows, header) + # Check not quoted and comma as delimiter + ctx.search_in_file(os.path.join(BOM_DIR, out), ['"'+KIBOM_TEST_HEAD[0]+'","'+KIBOM_TEST_HEAD[1]+'"']) + ctx.clean_up() + + +def test_int_bom_simple_txt(): + ctx, out = kibom_setup('int_bom_simple_txt', 'txt') + rows, header = ctx.load_csv(out, delimiter='\t') + kibom_verif(rows, header) + # Check all quoted and tab as delimiter + ctx.search_in_file(os.path.join(BOM_DIR, out), ['"'+KIBOM_TEST_HEAD[0]+'"\t"'+KIBOM_TEST_HEAD[1]+'"']) ctx.clean_up() @@ -282,19 +310,12 @@ def adapt_xml(h): def test_int_bom_simple_xml(): - prj = 'kibom-test' - ext = 'xml' - ctx = context.TestContextSCH('test_int_bom_simple_xml', prj, 'int_bom_simple_xml', BOM_DIR) - ctx.run() - out = prj + '-bom.' + ext + ctx, out = kibom_setup('int_bom_simple_xml', 'xml') rows, header = ctx.load_xml(out) # Columns get sorted by name, so we need to take care of it for c in KIBOM_TEST_HEAD: assert adapt_xml(c) in header, "Missing column "+c - ref_column = header.index(adapt_xml(REF_COLUMN_NAME)) - qty_column = header.index(adapt_xml(QTY_COLUMN_NAME)) - check_kibom_test_netlist(rows, ref_column, KIBOM_TEST_GROUPS, KIBOM_TEST_EXCLUDE, KIBOM_TEST_COMPONENTS) - check_dnc(rows, 'R7', ref_column, qty_column) + kibom_verif(rows, header, skip_head=True, qty_name=adapt_xml(QTY_COLUMN_NAME)) ctx.clean_up() diff --git a/tests/utils/context.py b/tests/utils/context.py index 5e5ca342..9fe9c909 100644 --- a/tests/utils/context.py +++ b/tests/utils/context.py @@ -269,9 +269,10 @@ class TestContext(object): txt = f.read() res = [] for t in texts: - logging.debug('- r"'+t+'"') + msg = '- r"'+t+'"' m = re.search(t, txt, re.MULTILINE) - assert m + assert m, msg + logging.debug(msg+' OK') # logging.debug(' '+m.group(0)) res.append(m.groups()) return res @@ -389,10 +390,10 @@ class TestContext(object): logging.debug("Found apertures {}".format(aps)) return aps - def load_csv(self, filename): + def load_csv(self, filename, delimiter=','): rows = [] with open(self.expect_out_file(os.path.join(self.sub_dir, filename))) as csvfile: - reader = csv.reader(csvfile) + reader = csv.reader(csvfile, delimiter=delimiter) header = next(reader) for r in reader: if not r: diff --git a/tests/yaml_samples/int_bom_refuse_no_sep.kiplot.yaml b/tests/yaml_samples/int_bom_refuse_no_sep.kiplot.yaml new file mode 100644 index 00000000..e9e72d13 --- /dev/null +++ b/tests/yaml_samples/int_bom_refuse_no_sep.kiplot.yaml @@ -0,0 +1,13 @@ +# Example KiPlot config file +kiplot: + version: 1 + +outputs: + - name: 'bom_internal' + comment: "Bill of Materials in CSV format" + type: bom + dir: BoM + options: + csv: + separator: '' + quote_all: true diff --git a/tests/yaml_samples/int_bom_simple_txt.kiplot.yaml b/tests/yaml_samples/int_bom_simple_txt.kiplot.yaml new file mode 100644 index 00000000..03a0f4af --- /dev/null +++ b/tests/yaml_samples/int_bom_simple_txt.kiplot.yaml @@ -0,0 +1,13 @@ +# Example KiPlot config file +kiplot: + version: 1 + +outputs: + - name: 'bom_internal' + comment: "Bill of Materials in CSV format" + type: bom + dir: BoM + options: + format: TXT + csv: + quote_all: true