Avoid mktemp in tests, in straightforward cases · EliahKagan/GitPython@41fac85 (original) (raw)

`@@ -359,48 +359,52 @@ def test_it_should_dashify(self):

`

359

359

`self.assertEqual("foo", dashify("foo"))

`

360

360

``

361

361

`def test_lock_file(self):

`

362

``

`-

my_file = tempfile.mktemp()

`

363

``

`-

lock_file = LockFile(my_file)

`

364

``

`-

assert not lock_file._has_lock()

`

365

``

`-

Release lock we don't have - fine.

`

366

``

`-

lock_file._release_lock()

`

``

362

`+

with tempfile.TemporaryDirectory() as tdir:

`

``

363

`+

my_file = os.path.join(tdir, "my-lock-file")

`

``

364

`+

lock_file = LockFile(my_file)

`

``

365

`+

assert not lock_file._has_lock()

`

``

366

`+

Release lock we don't have - fine.

`

``

367

`+

lock_file._release_lock()

`

367

368

``

368

``

`-

Get lock.

`

369

``

`-

lock_file._obtain_lock_or_raise()

`

370

``

`-

assert lock_file._has_lock()

`

``

369

`+

Get lock.

`

``

370

`+

lock_file._obtain_lock_or_raise()

`

``

371

`+

assert lock_file._has_lock()

`

371

372

``

372

``

`-

Concurrent access.

`

373

``

`-

other_lock_file = LockFile(my_file)

`

374

``

`-

assert not other_lock_file._has_lock()

`

375

``

`-

self.assertRaises(IOError, other_lock_file._obtain_lock_or_raise)

`

``

373

`+

Concurrent access.

`

``

374

`+

other_lock_file = LockFile(my_file)

`

``

375

`+

assert not other_lock_file._has_lock()

`

``

376

`+

self.assertRaises(IOError, other_lock_file._obtain_lock_or_raise)

`

376

377

``

377

``

`-

lock_file._release_lock()

`

378

``

`-

assert not lock_file._has_lock()

`

``

378

`+

lock_file._release_lock()

`

``

379

`+

assert not lock_file._has_lock()

`

379

380

``

380

``

`-

other_lock_file._obtain_lock_or_raise()

`

381

``

`-

self.assertRaises(IOError, lock_file._obtain_lock_or_raise)

`

``

381

`+

other_lock_file._obtain_lock_or_raise()

`

``

382

`+

self.assertRaises(IOError, lock_file._obtain_lock_or_raise)

`

382

383

``

383

``

`-

Auto-release on destruction.

`

384

``

`-

del other_lock_file

`

385

``

`-

lock_file._obtain_lock_or_raise()

`

386

``

`-

lock_file._release_lock()

`

``

384

`+

Auto-release on destruction.

`

``

385

`+

del other_lock_file

`

``

386

`+

lock_file._obtain_lock_or_raise()

`

``

387

`+

lock_file._release_lock()

`

387

388

``

388

389

`def test_blocking_lock_file(self):

`

389

``

`-

my_file = tempfile.mktemp()

`

390

``

`-

lock_file = BlockingLockFile(my_file)

`

391

``

`-

lock_file._obtain_lock()

`

392

``

-

393

``

`-

Next one waits for the lock.

`

394

``

`-

start = time.time()

`

395

``

`-

wait_time = 0.1

`

396

``

`-

wait_lock = BlockingLockFile(my_file, 0.05, wait_time)

`

397

``

`-

self.assertRaises(IOError, wait_lock._obtain_lock)

`

398

``

`-

elapsed = time.time() - start

`

``

390

`+

with tempfile.TemporaryDirectory() as tdir:

`

``

391

`+

my_file = os.path.join(tdir, "my-lock-file")

`

``

392

`+

lock_file = BlockingLockFile(my_file)

`

``

393

`+

lock_file._obtain_lock()

`

``

394

+

``

395

`+

Next one waits for the lock.

`

``

396

`+

start = time.time()

`

``

397

`+

wait_time = 0.1

`

``

398

`+

wait_lock = BlockingLockFile(my_file, 0.05, wait_time)

`

``

399

`+

self.assertRaises(IOError, wait_lock._obtain_lock)

`

``

400

`+

elapsed = time.time() - start

`

``

401

+

399

402

`extra_time = 0.02

`

400

403

`if os.name == "nt" or sys.platform == "cygwin":

`

401

404

`extra_time *= 6 # Without this, we get indeterministic failures on Windows.

`

402

405

`elif sys.platform == "darwin":

`

403

406

`extra_time *= 18 # The situation on macOS is similar, but with more delay.

`

``

407

+

404

408

`self.assertLess(elapsed, wait_time + extra_time)

`

405

409

``

406

410

`def test_user_id(self):

`