From 625de04adc097cecf9d05d3d50c272b60140cfd3 Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Sun, 15 Mar 2020 18:05:55 +0545 Subject: [PATCH] tests: remove use of mocker as context-manager --- tests/func/test_ls.py | 6 ++-- tests/unit/command/test_imp_url.py | 20 +++++------ tests/unit/test_logger.py | 58 +++++++++++++++--------------- tests/unit/utils/test_utils.py | 5 ++- 4 files changed, 43 insertions(+), 46 deletions(-) diff --git a/tests/func/test_ls.py b/tests/func/test_ls.py index 4b61f63985..fc44b73096 100644 --- a/tests/func/test_ls.py +++ b/tests/func/test_ls.py @@ -79,9 +79,9 @@ def test_ls_repo_with_color(tmp_dir, dvc, scm, mocker, monkeypatch, caplog): cmd = cli_args.func(cli_args) caplog.clear() - with mocker.patch("sys.stdout.isatty", return_value=True): - with caplog.at_level(logging.INFO, logger="dvc.command.ls"): - assert cmd.run() == 0 + mocker.patch("sys.stdout.isatty", return_value=True) + with caplog.at_level(logging.INFO, logger="dvc.command.ls"): + assert cmd.run() == 0 assert caplog.records[-1].msg == "\n".join( [ diff --git a/tests/unit/command/test_imp_url.py b/tests/unit/command/test_imp_url.py index df8e20ee99..8e04d12686 100644 --- a/tests/unit/command/test_imp_url.py +++ b/tests/unit/command/test_imp_url.py @@ -22,14 +22,12 @@ def test_failed_import_url(mocker, caplog): assert cli_args.func == CmdImportUrl cmd = cli_args.func(cli_args) - with mocker.patch.object( - cmd.repo, "imp_url", side_effect=DvcException("error") - ): - with caplog.at_level(logging.ERROR, logger="dvc"): - assert cmd.run() == 1 - expected_error = ( - "failed to import http://somesite.com/file_name. " - "You could also try downloading it manually, and " - "adding it with `dvc add`." - ) - assert expected_error in caplog.text + mocker.patch.object(cmd.repo, "imp_url", side_effect=DvcException("error")) + with caplog.at_level(logging.ERROR, logger="dvc"): + assert cmd.run() == 1 + expected_error = ( + "failed to import http://somesite.com/file_name. " + "You could also try downloading it manually, and " + "adding it with `dvc add`." + ) + assert expected_error in caplog.text diff --git a/tests/unit/test_logger.py b/tests/unit/test_logger.py index e16323eaa7..02a44bab89 100644 --- a/tests/unit/test_logger.py +++ b/tests/unit/test_logger.py @@ -24,10 +24,10 @@ @pytest.fixture() def dt(mocker): - with mocker.patch( + mocker.patch( "time.time", return_value=time.mktime(datetime(2020, 2, 2).timetuple()) - ): - yield "2020-02-02 00:00:00,000" + ) + yield "2020-02-02 00:00:00,000" class TestColorFormatter: @@ -195,32 +195,32 @@ def test_nested_exceptions(self, caplog, dt): def test_progress_awareness(self, mocker, capsys, caplog): from dvc.progress import Tqdm - with mocker.patch("sys.stdout.isatty", return_value=True): - with Tqdm(total=100, desc="progress") as pbar: - pbar.update() - - # logging an invisible message should not break - # the progress bar output - with caplog.at_level(logging.INFO, logger="dvc"): - debug_record = logging.LogRecord( - name="dvc", - level=logging.DEBUG, - pathname=__name__, - lineno=1, - msg="debug", - args=(), - exc_info=None, - ) - - formatter.format(debug_record) - captured = capsys.readouterr() - assert captured.out == "" - - # when the message is actually visible - with caplog.at_level(logging.INFO, logger="dvc"): - logger.info("some info") - captured = capsys.readouterr() - assert captured.out == "" + mocker.patch("sys.stdout.isatty", return_value=True) + with Tqdm(total=100, desc="progress") as pbar: + pbar.update() + + # logging an invisible message should not break + # the progress bar output + with caplog.at_level(logging.INFO, logger="dvc"): + debug_record = logging.LogRecord( + name="dvc", + level=logging.DEBUG, + pathname=__name__, + lineno=1, + msg="debug", + args=(), + exc_info=None, + ) + + formatter.format(debug_record) + captured = capsys.readouterr() + assert captured.out == "" + + # when the message is actually visible + with caplog.at_level(logging.INFO, logger="dvc"): + logger.info("some info") + captured = capsys.readouterr() + assert captured.out == "" def test_handlers(): diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py index 62f31446b2..196da51656 100644 --- a/tests/unit/utils/test_utils.py +++ b/tests/unit/utils/test_utils.py @@ -125,7 +125,6 @@ def test_relpath(): ], ) def test_resolve_output(inp, out, is_dir, expected, mocker): - with mocker.patch("os.path.isdir", return_value=is_dir): - result = resolve_output(inp, out) - + mocker.patch("os.path.isdir", return_value=is_dir) + result = resolve_output(inp, out) assert result == expected