Skip to content

Commit

Permalink
update: xiaohong split
Browse files Browse the repository at this point in the history
clean xiaoheng
  • Loading branch information
terryyz authored May 7, 2024
2 parents cbdaac3 + a27718e commit 6850246
Show file tree
Hide file tree
Showing 76 changed files with 2,074 additions and 1,613 deletions.
21 changes: 13 additions & 8 deletions data/raw/f_615_xiaoheng.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import sys
import re

def f_615(log_file_path: str, keywords: list):
Expand All @@ -17,7 +16,6 @@ def f_615(log_file_path: str, keywords: list):
Requirements:
- os
- re
- sys
Example:
>>> f_615('/path/to/log_file.log', ['ERROR', 'WARNING'])
Expand All @@ -43,19 +41,26 @@ def f_615(log_file_path: str, keywords: list):

import unittest
import os
import shutil

class TestCases(unittest.TestCase):

def setUp(self):
@classmethod
def setUpClass(cls):
# Setup code to create a test log file
self.test_file_path = "test_log_file.log"
with open(self.test_file_path, 'w') as f:
cls.test_file_path = "test_log_file.log"
with open(cls.test_file_path, 'w') as f:
f.write("ERROR 11:30:10 This is an error message\n")
f.write("WARNING 11:35:10 This is a warning message\n")

def tearDown(self):
@classmethod
def tearDownClass(cls):
# Cleanup the test directory and all its contents
shutil.rmtree(cls.test_dir)

@classmethod
def tearDownClass(cls):
# Cleanup the test log file
os.remove(self.test_file_path)
os.remove(cls.test_file_path)

def test_nonexistent_file(self):
with self.assertRaises(FileNotFoundError):
Expand Down
9 changes: 2 additions & 7 deletions data/raw/f_616_xiaoheng.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import unittest
import os
import sys
from pathlib import Path
import shutil

# Constants
PATH_TO_APPEND = '/path/to/whatever'
Expand All @@ -21,12 +18,9 @@ def f_616(path_to_append=PATH_TO_APPEND):
- path_to_append (str): The path that was appended and where the directory was created.
Requirements:
- unittest
- os
- sys
- pathlib
- shutil
Examples:
>>> f_616("/new/path/to/append")
"/new/path/to/append"
Expand All @@ -44,6 +38,7 @@ def f_616(path_to_append=PATH_TO_APPEND):
return path_to_append

import tempfile
import unittest

def run_tests():
suite = unittest.TestSuite()
Expand Down
54 changes: 27 additions & 27 deletions data/raw/f_617_xiaoheng.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
import os
import subprocess

# Constants
Expand All @@ -21,7 +20,6 @@ def f_617(python_version=PYTHON_VERSION, path_to_append=PATH_TO_APPEND):
Requirements:
- sys
- os
- subprocess
Example:
Expand All @@ -33,55 +31,57 @@ def f_617(python_version=PYTHON_VERSION, path_to_append=PATH_TO_APPEND):

return python_version

import sys
import unittest
from unittest.mock import patch

class TestCases(unittest.TestCase):

@patch('subprocess.run')
@patch('sys.path.append')
def test_switch_to_default_python_version(self, mock_append, mock_run):
result = f_617()
def test_switch_to_default_python_version(self, mock_run):
original_path_length = len(sys.path)
f_617()
mock_run.assert_called_with(['pyenv', 'global', '3.8'], check=True)
mock_append.assert_called_with('/path/to/whatever')
self.assertEqual(result, '3.8')
self.assertEqual(sys.path[-1], '/path/to/whatever')
sys.path = sys.path[:original_path_length] # Reset sys.path to original state

@patch('subprocess.run')
@patch('sys.path.append')
def test_switch_to_python_3_7(self, mock_append, mock_run):
result = f_617('3.7', '/another/path')
def test_switch_to_python_3_7(self, mock_run):
original_path_length = len(sys.path)
f_617('3.7', '/another/path')
mock_run.assert_called_with(['pyenv', 'global', '3.7'], check=True)
mock_append.assert_called_with('/another/path')
self.assertEqual(result, '3.7')
self.assertEqual(sys.path[-1], '/another/path')
sys.path = sys.path[:original_path_length]

@patch('subprocess.run')
@patch('sys.path.append')
def test_switch_to_python_3_9(self, mock_append, mock_run):
result = f_617('3.9')
def test_switch_to_python_3_9(self, mock_run):
original_path_length = len(sys.path)
f_617('3.9')
mock_run.assert_called_with(['pyenv', 'global', '3.9'], check=True)
mock_append.assert_called_with('/path/to/whatever')
self.assertEqual(result, '3.9')
self.assertEqual(sys.path[-1], '/path/to/whatever')
sys.path = sys.path[:original_path_length]

@patch('subprocess.run')
@patch('sys.path.append')
def test_switch_to_python_2_7(self, mock_append, mock_run):
result = f_617('2.7')
def test_switch_to_python_2_7(self, mock_run):
original_path_length = len(sys.path)
f_617('2.7')
mock_run.assert_called_with(['pyenv', 'global', '2.7'], check=True)
mock_append.assert_called_with('/path/to/whatever')
self.assertEqual(result, '2.7')
self.assertEqual(sys.path[-1], '/path/to/whatever')
sys.path = sys.path[:original_path_length]

@patch('subprocess.run')
@patch('sys.path.append')
def test_switch_to_python_3_6(self, mock_append, mock_run):
result = f_617('3.6', '/different/path')
def test_switch_to_python_3_6(self, mock_run):
original_path_length = len(sys.path)
f_617('3.6', '/different/path')
mock_run.assert_called_with(['pyenv', 'global', '3.6'], check=True)
mock_append.assert_called_with('/different/path')
self.assertEqual(result, '3.6')
self.assertEqual(sys.path[-1], '/different/path')
sys.path = sys.path[:original_path_length]

def run_tests():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestCases))
runner = unittest.TextTestRunner()
runner.run(suite)

if __name__ == "__main__":
run_tests()
35 changes: 23 additions & 12 deletions data/raw/f_618_xiaoheng.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import sys
import json
from datetime import datetime
Expand All @@ -20,7 +19,6 @@ def f_618(path_to_append=PATH_TO_APPEND, json_file=JSON_FILE):
- json_data (dict): The updated JSON data. The dictionary will contain a 'last_updated' key with the current datetime as its value.
Requirements:
- os
- sys
- json
- datetime.datetime
Expand All @@ -43,14 +41,29 @@ def f_618(path_to_append=PATH_TO_APPEND, json_file=JSON_FILE):
import unittest
import json
import os
from datetime import datetime
import tempfile
import sys
from datetime import datetime

# Update this path if needed to point to an actual temporary directory
PATH_TO_TEMP_JSON = tempfile.mktemp(suffix='.json')

class TestCases(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Create a temporary JSON file for tests that rely on the default JSON file
with open(PATH_TO_TEMP_JSON, 'w') as f:
json.dump({'initial_key': 'initial_value'}, f)

@classmethod
def tearDownClass(cls):
# Clean up the temporary JSON file after all tests have run
os.remove(PATH_TO_TEMP_JSON)

def setUp(self):
# Create temporary JSON files for testing
self.test_json_file_1 = tempfile.NamedTemporaryFile(delete=False)
self.test_json_file_2 = tempfile.NamedTemporaryFile(delete=False)
# Create temporary JSON files for testing in text mode
self.test_json_file_1 = tempfile.NamedTemporaryFile(mode='w+', delete=False)
self.test_json_file_2 = tempfile.NamedTemporaryFile(mode='w+', delete=False)
json.dump({'key': 'value'}, self.test_json_file_1)
json.dump({'key': 'value'}, self.test_json_file_2)
self.test_json_file_1.close()
Expand All @@ -64,7 +77,7 @@ def tearDown(self):
def test_path_append(self):
# Test if the path is correctly appended to sys.path
new_path = '/new/test/path'
f_618(path_to_append=new_path)
f_618(path_to_append=new_path, json_file=self.test_json_file_1.name)
self.assertIn(new_path, sys.path)

def test_json_update_1(self):
Expand All @@ -81,14 +94,12 @@ def test_json_update_2(self):

def test_default_path(self):
# Test if the default path is correctly appended when no argument is passed
f_618()
self.assertIn(PATH_TO_APPEND, sys.path)
f_618(json_file=self.test_json_file_1.name)
self.assertIn('/path/to/whatever', sys.path)

def test_default_json(self):
# Test if the default JSON file is correctly updated when no argument is passed
with open(JSON_FILE, 'w') as file:
json.dump({'default_key': 'default_value'}, file, indent=4)
output = f_618()
output = f_618(json_file=PATH_TO_TEMP_JSON)
self.assertIn('last_updated', output)
self.assertIsInstance(datetime.strptime(output['last_updated'], '%Y-%m-%d %H:%M:%S.%f'), datetime)

Expand Down
53 changes: 37 additions & 16 deletions data/raw/f_619_xiaoheng.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def f_619(path_to_append=PATH_TO_APPEND, config_file=CONFIG_FILE):
- config_file (str): The path to the configuration file that was just modified.
Requirements:
- os
- sys
- configparser.ConfigParser
Expand All @@ -27,11 +26,22 @@ def f_619(path_to_append=PATH_TO_APPEND, config_file=CONFIG_FILE):
>>> 'path_to_append' in config['DEFAULT']
True
"""
sys.path.append(path_to_append)
if isinstance(path_to_append, list):
for path in path_to_append:
sys.path.append(path)
else:
sys.path.append(path_to_append)

config = ConfigParser()

# Create the file if it doesn't exist
if not os.path.exists(config_file):
open(config_file, 'a').close()

config.read(config_file)
config.set('DEFAULT', 'path_to_append', path_to_append)
path_str = ','.join(path_to_append) if isinstance(path_to_append, list) else path_to_append
config.set('DEFAULT', 'path_to_append', path_str)

with open(config_file, 'w') as file:
config.write(file)

Expand All @@ -40,58 +50,69 @@ def f_619(path_to_append=PATH_TO_APPEND, config_file=CONFIG_FILE):
import unittest
import os
import sys
import tempfile
from configparser import ConfigParser

class TestCases(unittest.TestCase):
def setUp(self):
# Create a temporary configuration file for testing
self.temp_config_file = 'temp_config.ini'
self.temp_config_file = tempfile.NamedTemporaryFile(delete=False, mode='w')
config = ConfigParser()
config['DEFAULT'] = {'setting1': 'value1', 'setting2': 'value2'}
with open(self.temp_config_file, 'w') as file:
config.write(file)
config.write(self.temp_config_file)
self.temp_config_file.close()

def tearDown(self):
os.remove(self.temp_config_file)
os.remove(self.temp_config_file.name)

def test_append_path_and_update_config(self):
new_path = '/path/to/test/directory'
updated_config, config_file_path = f_619(new_path, self.temp_config_file)
updated_config, config_file_path = f_619(new_path, self.temp_config_file.name)
self.assertIn(new_path, sys.path)
self.assertEqual(updated_config['DEFAULT']['path_to_append'], new_path)
self.assertEqual(config_file_path, self.temp_config_file)
self.assertEqual(config_file_path, self.temp_config_file.name)

def test_default_path_and_config(self):
updated_config, config_file_path = f_619()
updated_config, config_file_path = f_619(PATH_TO_APPEND, self.temp_config_file.name)
self.assertIn(PATH_TO_APPEND, sys.path)
self.assertEqual(updated_config['DEFAULT']['path_to_append'], PATH_TO_APPEND)
self.assertEqual(config_file_path, CONFIG_FILE)
self.assertEqual(config_file_path, self.temp_config_file.name)

def test_invalid_config_file(self):
invalid_config_file = 'invalid_config.ini'
with self.assertRaises(FileNotFoundError):
f_619(config_file=invalid_config_file)
if os.path.exists(invalid_config_file):
os.remove(invalid_config_file) # Ensure the file does not exist before the test
try:
updated_config, config_file_path = f_619(config_file=invalid_config_file)
self.assertTrue(os.path.exists(invalid_config_file), "The config file should be created.")
finally:
if os.path.exists(invalid_config_file):
os.remove(invalid_config_file) # Clean up the created file


def test_config_file_creation(self):
new_config_file = 'new_config.ini'
if os.path.exists(new_config_file):
os.remove(new_config_file) # Ensure the file does not exist before the test
updated_config, config_file_path = f_619(config_file=new_config_file)
self.assertTrue(os.path.exists(new_config_file))
os.remove(new_config_file)


def test_multiple_paths(self):
path1 = '/path/to/test/directory1'
path2 = '/path/to/test/directory2'
updated_config, config_file_path = f_619(path_to_append=[path1, path2], config_file=self.temp_config_file)
updated_config, config_file_path = f_619(path_to_append=[path1, path2], config_file=self.temp_config_file.name)
self.assertIn(path1, sys.path)
self.assertIn(path2, sys.path)
self.assertEqual(updated_config['DEFAULT']['path_to_append'], f"{path1},{path2}")
self.assertEqual(config_file_path, self.temp_config_file)
self.assertEqual(config_file_path, self.temp_config_file.name)

def run_tests():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestCases))
runner = unittest.TextTestRunner()
runner.run(suite)

if __name__ == "__main__":
run_tests()
Loading

0 comments on commit 6850246

Please sign in to comment.