From b51144b2bcc90bbf8a147c6a9e85cc009869bccb Mon Sep 17 00:00:00 2001 From: rohilla21 Date: Wed, 4 Jul 2018 00:25:52 +0530 Subject: [PATCH] generate_package.py: Replace touch This replaces the user-defined touch function with library function Path. Closes https://github.com/coala/coala-bears/issues/2575 --- bears/generate_package.py | 12 ++---------- tests/generate_packageTest.py | 10 ++++++---- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/bears/generate_package.py b/bears/generate_package.py index a43de8ae8a..9fa36370de 100644 --- a/bears/generate_package.py +++ b/bears/generate_package.py @@ -2,6 +2,7 @@ import glob import os import shutil +from pathlib import Path from string import Template import subprocess import sys @@ -13,15 +14,6 @@ from coalib.parsing.Globbing import glob -def touch(file_name): - """ - Creates an empty file. An existing file remains untouched. - - :param file_name: Name of the file. - """ - open(file_name, 'a').close() - - def create_file_from_template(template_file, output_file, substitution_dict): """ Creates a file from a template file, using a substitution dict. @@ -55,7 +47,7 @@ def create_file_structure_for_packages(root_folder, file_to_copy, object_name): upload_package_folder = os.path.join( root_folder, object_name, 'coala' + object_name) os.makedirs(upload_package_folder, exist_ok=True) - touch(os.path.join(upload_package_folder, '__init__.py')) + Path(os.path.join(upload_package_folder, '__init__.py')).touch() shutil.copyfile(file_to_copy, os.path.join(upload_package_folder, object_name + '.py')) diff --git a/tests/generate_packageTest.py b/tests/generate_packageTest.py index ad29c8af41..1c38f299c7 100644 --- a/tests/generate_packageTest.py +++ b/tests/generate_packageTest.py @@ -2,8 +2,10 @@ import shutil import sys import unittest +from pathlib import Path from unittest.mock import patch -from bears.generate_package import (VERSION, touch, create_file_from_template, + +from bears.generate_package import (VERSION, create_file_from_template, create_file_structure_for_packages, perform_register, perform_upload, main, create_upload_parser) @@ -17,7 +19,7 @@ def setUp(self): def test_file_doesnt_exist(self): self.assertFalse(os.path.exists('TestFile.py')) - touch('TestFile.py') + Path('TestFile.py').touch() self.assertTrue(os.path.exists('TestFile.py')) def tearDown(self): @@ -52,7 +54,7 @@ class CreateFileStructureForPackagesTest(unittest.TestCase): BEAR_FILE_PATH = os.path.join('folder', 'Test', 'coalaTest', 'Test.py') def test_structure(self): - touch('TestFile.py') + Path('TestFile.py').touch() create_file_structure_for_packages('folder', 'TestFile.py', 'Test') self.assertTrue(os.path.exists(self.INIT_FILE_PATH)) self.assertTrue(os.path.exists(self.BEAR_FILE_PATH)) @@ -147,7 +149,7 @@ def test_register(self, call_mock): def test_no_bear_object(self): if not os.path.exists(self.NO_BEAR_PATH): os.makedirs(os.path.join(self.BEARS_PATH, 'BadBear')) - touch(self.NO_BEAR_PATH) + Path(self.NO_BEAR_PATH).touch() main() self.assertFalse(os.path.exists(os.path.join( self.BEARS_UPLOAD_PATH, 'BadBear')))