diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 37253370022..cc4a73c51ab 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -671,7 +671,11 @@ jobs: brew unlink python@$python_version && brew link --overwrite python@$python_version echo == Remove pre-installed azure-cli == - brew uninstall azure-cli + if brew list --versions azure-cli >/dev/null 2>&1; then + brew uninstall azure-cli + else + echo "azure-cli is not pre-installed" + fi echo == Install azure-cli.rb formula == # Need to create a dummy homebrew tap to install the formula diff --git a/scripts/release/homebrew/test_homebrew_package.sh b/scripts/release/homebrew/test_homebrew_package.sh index c0471691cef..31abf3038cc 100755 --- a/scripts/release/homebrew/test_homebrew_package.sh +++ b/scripts/release/homebrew/test_homebrew_package.sh @@ -5,7 +5,11 @@ set -ev CLI_VERSION=`cat $SYSTEM_ARTIFACTSDIRECTORY/metadata/version` echo == Remove pre-installed azure-cli == -brew uninstall azure-cli +if brew list --versions azure-cli >/dev/null 2>&1; then + brew uninstall azure-cli +else + echo "azure-cli is not pre-installed" +fi echo == Install azure-cli.rb formula == # TODO(packaging): remove once the macOS CI agent image's Homebrew provides formula_opt_prefix. diff --git a/src/azure-cli/azure/cli/command_modules/storage/operations/fs_file.py b/src/azure-cli/azure/cli/command_modules/storage/operations/fs_file.py index 27e61338691..110b9a1a5e1 100644 --- a/src/azure-cli/azure/cli/command_modules/storage/operations/fs_file.py +++ b/src/azure-cli/azure/cli/command_modules/storage/operations/fs_file.py @@ -43,8 +43,7 @@ def download_file(client, destination_path=None, overwrite=True, timeout=None): with open(destination_path, 'wb') as stream: download = client.download_file(timeout=timeout) - download_content = download.readall() - stream.write(download_content) + download.readinto(stream) def exists(cmd, client, timeout=None): diff --git a/src/azure-cli/azure/cli/command_modules/storage/tests/latest/test_storage_fs_file_operations.py b/src/azure-cli/azure/cli/command_modules/storage/tests/latest/test_storage_fs_file_operations.py new file mode 100644 index 00000000000..d007040fff2 --- /dev/null +++ b/src/azure-cli/azure/cli/command_modules/storage/tests/latest/test_storage_fs_file_operations.py @@ -0,0 +1,36 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- + +import os +import tempfile +import unittest +from types import SimpleNamespace +from unittest import mock + +from azure.cli.command_modules.storage.operations.fs_file import download_file + + +class TestStorageFsFileOperations(unittest.TestCase): + + def test_download_file_streams_content_to_destination(self): + download = mock.Mock() + + def _write_to_stream(target_stream): + return target_stream.write(b'hello world') + + download.readinto.side_effect = _write_to_stream + + client = mock.Mock() + client.get_file_properties.return_value = SimpleNamespace(name='dir/test.txt') + client.download_file.return_value = download + + with tempfile.TemporaryDirectory() as temp_dir: + download_file(client, destination_path=temp_dir) + + with open(os.path.join(temp_dir, 'test.txt'), 'rb') as downloaded_file: + self.assertEqual(downloaded_file.read(), b'hello world') + + download.readinto.assert_called_once() + download.readall.assert_not_called()