# Copyright 2013-2021 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import os

import pytest

from llnl.util.filesystem import mkdirp, touch

import spack.config
from spack.fetch_strategy import CacheURLFetchStrategy, NoCacheError
from spack.stage import Stage


@pytest.mark.parametrize('_fetch_method', ['curl', 'urllib'])
def test_fetch_missing_cache(tmpdir, _fetch_method):
    """Ensure raise a missing cache file."""
    testpath = str(tmpdir)
    with spack.config.override('config:url_fetch_method', _fetch_method):
        fetcher = CacheURLFetchStrategy(url='file:///not-a-real-cache-file')
        with Stage(fetcher, path=testpath):
            with pytest.raises(NoCacheError, match=r'No cache'):
                fetcher.fetch()


@pytest.mark.parametrize('_fetch_method', ['curl', 'urllib'])
def test_fetch(tmpdir, _fetch_method):
    """Ensure a fetch after expanding is effectively a no-op."""
    testpath = str(tmpdir)
    cache = os.path.join(testpath, 'cache.tar.gz')
    touch(cache)
    url = 'file:///{0}'.format(cache)
    with spack.config.override('config:url_fetch_method', _fetch_method):
        fetcher = CacheURLFetchStrategy(url=url)
        with Stage(fetcher, path=testpath) as stage:
            source_path = stage.source_path
            mkdirp(source_path)
            fetcher.fetch()

[((394, 454), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""_fetch_method"""', "['curl', 'urllib']"], {}), "('_fetch_method', ['curl', 'urllib'])\n", (417, 454), False, 'import pytest\n'), ((875, 935), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""_fetch_method"""', "['curl', 'urllib']"], {}), "('_fetch_method', ['curl', 'urllib'])\n", (898, 935), False, 'import pytest\n'), ((1079, 1117), 'os.path.join', 'os.path.join', (['testpath', '"""cache.tar.gz"""'], {}), "(testpath, 'cache.tar.gz')\n", (1091, 1117), False, 'import os\n'), ((1122, 1134), 'llnl.util.filesystem.touch', 'touch', (['cache'], {}), '(cache)\n', (1127, 1134), False, 'from llnl.util.filesystem import mkdirp, touch\n'), ((672, 730), 'spack.fetch_strategy.CacheURLFetchStrategy', 'CacheURLFetchStrategy', ([], {'url': '"""file:///not-a-real-cache-file"""'}), "(url='file:///not-a-real-cache-file')\n", (693, 730), False, 'from spack.fetch_strategy import CacheURLFetchStrategy, NoCacheError\n'), ((1265, 1295), 'spack.fetch_strategy.CacheURLFetchStrategy', 'CacheURLFetchStrategy', ([], {'url': 'url'}), '(url=url)\n', (1286, 1295), False, 'from spack.fetch_strategy import CacheURLFetchStrategy, NoCacheError\n'), ((744, 773), 'spack.stage.Stage', 'Stage', (['fetcher'], {'path': 'testpath'}), '(fetcher, path=testpath)\n', (749, 773), False, 'from spack.stage import Stage\n'), ((1309, 1338), 'spack.stage.Stage', 'Stage', (['fetcher'], {'path': 'testpath'}), '(fetcher, path=testpath)\n', (1314, 1338), False, 'from spack.stage import Stage\n'), ((1405, 1424), 'llnl.util.filesystem.mkdirp', 'mkdirp', (['source_path'], {}), '(source_path)\n', (1411, 1424), False, 'from llnl.util.filesystem import mkdirp, touch\n'), ((792, 837), 'pytest.raises', 'pytest.raises', (['NoCacheError'], {'match': '"""No cache"""'}), "(NoCacheError, match='No cache')\n", (805, 837), False, 'import pytest\n')]