|
46 | 46 | DUMMY_UNICODE_LENGTH = len(DUMMY_UNICODE) |
47 | 47 | DUMMY_FILE_WRITECHUNK = 'fileio/write_chunk' |
48 | 48 | DUMMY_FILE_WRITEFILE = 'fileio/write_file' |
49 | | -# TODO: add similar tests for write_file_lines and enable next |
50 | | -# DUMMY_FILE_WRITEFILELINES = 'fileio/write_file_lines' |
| 49 | +DUMMY_FILE_WRITEFILELINES = 'fileio/write_file_lines' |
51 | 50 | DUMMY_FILE_READFILE = 'fileio/read_file' |
52 | 51 | DUMMY_FILE_READFILELINES = 'fileio/read_file_lines' |
53 | 52 | DUMMY_FILE_READHEADLINES = 'fileio/read_head_lines' |
@@ -261,6 +260,58 @@ def test_store_unicode_in_binary_mode(self): |
261 | 260 | self.assertEqual(content[:], DUMMY_UNICODE) |
262 | 261 |
|
263 | 262 |
|
| 263 | +class MigSharedFileio__write_file_lines(MigTestCase): |
| 264 | + """Test the write_file_lines function from mig.shared.fileio module""" |
| 265 | + |
| 266 | + def setUp(self): |
| 267 | + """Initialize test environment for write_file_lines tests""" |
| 268 | + super(MigSharedFileio__write_file_lines, self).setUp() |
| 269 | + self.tmp_path = temppath(DUMMY_FILE_WRITEFILELINES, self) |
| 270 | + # Output dir is created by default here |
| 271 | + cleanpath(os.path.dirname(DUMMY_FILE_WRITEFILELINES), self) |
| 272 | + |
| 273 | + def test_write_lines(self): |
| 274 | + """Test write_file_lines writes lines to a file""" |
| 275 | + test_lines = ["line1\n", "line2\n", "line3"] |
| 276 | + result = fileio.write_file_lines( |
| 277 | + test_lines, self.tmp_path, self.logger) |
| 278 | + self.assertTrue(result) |
| 279 | + |
| 280 | + # Verify with read_file_lines |
| 281 | + lines = fileio.read_file_lines(self.tmp_path, self.logger) |
| 282 | + self.assertEqual(lines, test_lines) |
| 283 | + |
| 284 | + def test_invalid_data(self): |
| 285 | + """Test write_file_lines raises TypeError for non-list input""" |
| 286 | + self.logger.forgive_errors() |
| 287 | + with self.assertRaises(TypeError): |
| 288 | + fileio.write_file_lines(4242, self.tmp_path, self.logger) |
| 289 | + |
| 290 | + def test_creates_directory(self): |
| 291 | + """Test write_file_lines creates parent directory when needed""" |
| 292 | + test_lines = ["test line"] |
| 293 | + result = fileio.write_file_lines( |
| 294 | + test_lines, self.tmp_path, self.logger) |
| 295 | + self.assertTrue(result) |
| 296 | + |
| 297 | + path_kind = self.assertPathExists('fileio/write_file_lines') |
| 298 | + self.assertEqual(path_kind, "file") |
| 299 | + |
| 300 | + def test_return_false_on_invalid_dir(self): |
| 301 | + """Test write_file_lines returns False when path is directory""" |
| 302 | + self.logger.forgive_errors() |
| 303 | + os.makedirs(self.tmp_path) |
| 304 | + result = fileio.write_file_lines(["dummy"], self.tmp_path, self.logger) |
| 305 | + self.assertFalse(result) |
| 306 | + |
| 307 | + def test_return_false_on_missing_dir(self): |
| 308 | + """Test write_file_lines fails when parent directory missing""" |
| 309 | + self.logger.forgive_errors() |
| 310 | + result = fileio.write_file_lines(["dummy"], self.tmp_path, self.logger, |
| 311 | + make_parent=False) |
| 312 | + self.assertFalse(result) |
| 313 | + |
| 314 | + |
264 | 315 | class MigSharedFileio__read_file(MigTestCase): |
265 | 316 | """Test the read_file function from mig.shared.fileio module""" |
266 | 317 |
|
|
0 commit comments