From 85225538e69c6c9ce846813b62db154d3016539f Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Mon, 26 Feb 2024 01:23:49 +0100 Subject: [PATCH] Create test files in seperate folder & cleanup before image build (#6571) * move ignore * create testfiles in seperate folder * add cleanup step to docker build * use pathlib for paths --- .github/workflows/docker.yaml | 3 +++ .gitignore | 10 ---------- InvenTree/InvenTree/tests.py | 15 +++++++++++---- InvenTree/_testfolder/.gitignore | 8 ++++++++ InvenTree/part/test_api.py | 13 ++++++++----- InvenTree/part/test_bom_export.py | 5 +++-- InvenTree/plugin/base/label/test_label_mixin.py | 10 ++++++---- .../plugin/samples/integration/label_sample.py | 3 ++- 8 files changed, 41 insertions(+), 26 deletions(-) create mode 100644 InvenTree/_testfolder/.gitignore diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index 238d4c84f2..0e211ddaa1 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -103,6 +103,9 @@ jobs: docker-compose run inventree-dev-server invoke test --disable-pty docker-compose run inventree-dev-server invoke test --migrations --disable-pty docker-compose down + - name: Clean up test folder + run: | + rm -rf InvenTree/_testfolder - name: Set up QEMU if: github.event_name != 'pull_request' uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # pin@v3.0.0 diff --git a/.gitignore b/.gitignore index 6296696ebe..5f2b31d170 100644 --- a/.gitignore +++ b/.gitignore @@ -39,16 +39,6 @@ local_settings.py # Files used for testing inventree-demo-dataset/ inventree-data/ -dummy_image.* -_tmp.csv -InvenTree/label.pdf -InvenTree/label.png -InvenTree/part_image_123abc.png -label.pdf -label.png -InvenTree/my_special* -_tests*.txt -schema.yml # Local static and media file storage (only when running in development mode) inventree_media diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 4ffc2c0515..dedf608d7e 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -1042,9 +1042,12 @@ class TestSettings(InvenTreeTestCase): ) # with env set - with self.in_env_context({'INVENTREE_CONFIG_FILE': 'my_special_conf.yaml'}): + with self.in_env_context({ + 'INVENTREE_CONFIG_FILE': '_testfolder/my_special_conf.yaml' + }): self.assertIn( - 'inventree/my_special_conf.yaml', str(config.get_config_file()).lower() + 'inventree/_testfolder/my_special_conf.yaml', + str(config.get_config_file()).lower(), ) def test_helpers_plugin_file(self): @@ -1058,8 +1061,12 @@ class TestSettings(InvenTreeTestCase): ) # with env set - with self.in_env_context({'INVENTREE_PLUGIN_FILE': 'my_special_plugins.txt'}): - self.assertIn('my_special_plugins.txt', str(config.get_plugin_file())) + with self.in_env_context({ + 'INVENTREE_PLUGIN_FILE': '_testfolder/my_special_plugins.txt' + }): + self.assertIn( + '_testfolder/my_special_plugins.txt', str(config.get_plugin_file()) + ) def test_helpers_setting(self): """Test get_setting.""" diff --git a/InvenTree/_testfolder/.gitignore b/InvenTree/_testfolder/.gitignore new file mode 100644 index 0000000000..eeb6f71057 --- /dev/null +++ b/InvenTree/_testfolder/.gitignore @@ -0,0 +1,8 @@ +# Files used for testing +dummy_image.* +_tmp.csv +part_image_123abc.png +label.pdf +label.png +my_special* +_tests*.txt diff --git a/InvenTree/part/test_api.py b/InvenTree/part/test_api.py index a9146b2f57..6644a6cea7 100644 --- a/InvenTree/part/test_api.py +++ b/InvenTree/part/test_api.py @@ -4,6 +4,7 @@ import os from datetime import datetime from decimal import Decimal from enum import IntEnum +from pathlib import Path from random import randint from django.core.exceptions import ValidationError @@ -20,6 +21,7 @@ import company.models import order.models from common.models import InvenTreeSetting from company.models import Company, SupplierPart +from InvenTree.settings import BASE_DIR from InvenTree.status_codes import BuildStatus, PurchaseOrderStatusGroups, StockStatus from InvenTree.unit_test import InvenTreeAPITestCase from part.models import ( @@ -1478,10 +1480,11 @@ class PartDetailTests(PartAPITestBase): print(p.image.file) # Try to upload a non-image file - with open('dummy_image.txt', 'w') as dummy_image: + test_path = BASE_DIR / '_testfolder' / 'dummy_image' + with open(f'{test_path}.txt', 'w') as dummy_image: dummy_image.write('hello world') - with open('dummy_image.txt', 'rb') as dummy_image: + with open(f'{test_path}.txt', 'rb') as dummy_image: response = self.upload_client.patch( url, {'image': dummy_image}, format='multipart' ) @@ -1491,7 +1494,7 @@ class PartDetailTests(PartAPITestBase): # Now try to upload a valid image file, in multiple formats for fmt in ['jpg', 'j2k', 'png', 'bmp', 'webp']: - fn = f'dummy_image.{fmt}' + fn = f'{test_path}.{fmt}' img = PIL.Image.new('RGB', (128, 128), color='red') img.save(fn) @@ -1512,7 +1515,7 @@ class PartDetailTests(PartAPITestBase): # First, upload an image for an existing part p = Part.objects.first() - fn = 'part_image_123abc.png' + fn = BASE_DIR / '_testfolder' / 'part_image_123abc.png' img = PIL.Image.new('RGB', (128, 128), color='blue') img.save(fn) @@ -1557,7 +1560,7 @@ class PartDetailTests(PartAPITestBase): # First, upload an image for an existing part p = Part.objects.first() - fn = 'part_image_123abc.png' + fn = BASE_DIR / '_testfolder' / 'part_image_123abc.png' img = PIL.Image.new('RGB', (128, 128), color='blue') img.save(fn) diff --git a/InvenTree/part/test_bom_export.py b/InvenTree/part/test_bom_export.py index 4269b2b3c4..c1abe0de39 100644 --- a/InvenTree/part/test_bom_export.py +++ b/InvenTree/part/test_bom_export.py @@ -5,6 +5,7 @@ import csv from django.urls import reverse import part.models +from InvenTree.settings import BASE_DIR from InvenTree.unit_test import InvenTreeTestCase @@ -43,7 +44,7 @@ class BomExportTest(InvenTreeTestCase): 'attachment; filename="InvenTree_BOM_Template.csv"', ) - filename = '_tmp.csv' + filename = BASE_DIR / '_testfolder' / '_tmp.csv' with open(filename, 'wb') as f: f.write(response.getvalue()) @@ -89,7 +90,7 @@ class BomExportTest(InvenTreeTestCase): content = response.headers['Content-Disposition'] self.assertEqual(content, 'attachment; filename="BOB | Bob | A2_BOM.csv"') - filename = '_tmp.csv' + filename = BASE_DIR / '_testfolder' / '_tmp.csv' with open(filename, 'wb') as f: f.write(response.getvalue()) diff --git a/InvenTree/plugin/base/label/test_label_mixin.py b/InvenTree/plugin/base/label/test_label_mixin.py index 17d9a2343f..e7d6ec6cf1 100644 --- a/InvenTree/plugin/base/label/test_label_mixin.py +++ b/InvenTree/plugin/base/label/test_label_mixin.py @@ -10,6 +10,7 @@ from django.urls import reverse from pdfminer.high_level import extract_text from PIL import Image +from InvenTree.settings import BASE_DIR from InvenTree.unit_test import InvenTreeAPITestCase from label.models import PartLabel, StockItemLabel, StockLocationLabel from part.models import Part @@ -165,18 +166,19 @@ class LabelMixinTests(InvenTreeAPITestCase): # Test that the labels have been printed # The sample labelling plugin simply prints to file - self.assertTrue(os.path.exists('label.pdf')) + test_path = BASE_DIR / '_testfolder' / 'label' + self.assertTrue(os.path.exists(f'{test_path}.pdf')) # Read the raw .pdf data - ensure it contains some sensible information - filetext = extract_text('label.pdf') + filetext = extract_text(f'{test_path}.pdf') matched = [part.name in filetext for part in parts] self.assertIn(True, matched) # Check that the .png file has already been created - self.assertTrue(os.path.exists('label.png')) + self.assertTrue(os.path.exists(f'{test_path}.png')) # And that it is a valid image file - Image.open('label.png') + Image.open(f'{test_path}.png') def test_printing_options(self): """Test printing options.""" diff --git a/InvenTree/plugin/samples/integration/label_sample.py b/InvenTree/plugin/samples/integration/label_sample.py index 5ac88f532b..7e7c5c8645 100644 --- a/InvenTree/plugin/samples/integration/label_sample.py +++ b/InvenTree/plugin/samples/integration/label_sample.py @@ -5,6 +5,7 @@ This does not function in real usage and is more to show the required components from rest_framework import serializers +from InvenTree.settings import BASE_DIR from plugin import InvenTreePlugin from plugin.mixins import LabelPrintingMixin @@ -35,7 +36,7 @@ class SampleLabelPrinter(LabelPrintingMixin, InvenTreePlugin): pdf_data = kwargs['pdf_data'] png_file = self.render_to_png(label=None, pdf_data=pdf_data) - filename = 'label.pdf' + filename = str(BASE_DIR / '_testfolder' / 'label.pdf') # Dump the PDF to a local file with open(filename, 'wb') as pdf_out: