summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/cmd/url.py
blob: f96177d2e5c5ab4384e8ca68fbf21bfebb34f0fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
##############################################################################
# Copyright (c) 2013-2018, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/spack/spack
# Please also see the NOTICE and LICENSE files for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
import re
import pytest

import spack.repo
from spack.url import UndetectableVersionError
from spack.main import SpackCommand
from spack.cmd.url import name_parsed_correctly, version_parsed_correctly
from spack.cmd.url import url_summary

url = SpackCommand('url')


class MyPackage:
    def __init__(self, name, versions):
        self.name = name
        self.versions = versions


def test_name_parsed_correctly():
    # Expected True
    assert name_parsed_correctly(MyPackage('netcdf',         []), 'netcdf')
    assert name_parsed_correctly(MyPackage('r-devtools',     []), 'devtools')
    assert name_parsed_correctly(MyPackage('py-numpy',       []), 'numpy')
    assert name_parsed_correctly(MyPackage('octave-splines', []), 'splines')
    assert name_parsed_correctly(MyPackage('th-data',        []), 'TH.data')
    assert name_parsed_correctly(
        MyPackage('imagemagick',    []), 'ImageMagick')

    # Expected False
    assert not name_parsed_correctly(MyPackage('',            []), 'hdf5')
    assert not name_parsed_correctly(MyPackage('hdf5',        []), '')
    assert not name_parsed_correctly(MyPackage('yaml-cpp',    []), 'yamlcpp')
    assert not name_parsed_correctly(MyPackage('yamlcpp',     []), 'yaml-cpp')
    assert not name_parsed_correctly(MyPackage('r-py-parser', []), 'parser')
    assert not name_parsed_correctly(
        MyPackage('oce',         []), 'oce-0.18.0')


def test_version_parsed_correctly():
    # Expected True
    assert version_parsed_correctly(MyPackage('', ['1.2.3']),        '1.2.3')
    assert version_parsed_correctly(MyPackage('', ['5.4a', '5.4b']), '5.4a')
    assert version_parsed_correctly(MyPackage('', ['5.4a', '5.4b']), '5.4b')
    assert version_parsed_correctly(MyPackage('', ['1.63.0']),       '1_63_0')
    assert version_parsed_correctly(MyPackage('', ['0.94h']),        '094h')

    # Expected False
    assert not version_parsed_correctly(MyPackage('', []),         '1.2.3')
    assert not version_parsed_correctly(MyPackage('', ['1.2.3']),  '')
    assert not version_parsed_correctly(MyPackage('', ['1.2.3']),  '1.2.4')
    assert not version_parsed_correctly(MyPackage('', ['3.4a']),   '3.4')
    assert not version_parsed_correctly(MyPackage('', ['3.4']),    '3.4b')
    assert not version_parsed_correctly(
        MyPackage('', ['0.18.0']), 'oce-0.18.0')


def test_url_parse():
    url('parse', 'http://zlib.net/fossils/zlib-1.2.10.tar.gz')


def test_url_with_no_version_fails():
    # No version in URL
    with pytest.raises(UndetectableVersionError):
        url('parse', 'http://www.netlib.org/voronoi/triangle.zip')


@pytest.mark.network
def test_url_list():
    out = url('list')
    total_urls = len(out.split('\n'))

    # The following two options should not change the number of URLs printed.
    out = url('list', '--color', '--extrapolation')
    colored_urls = len(out.split('\n'))
    assert colored_urls == total_urls

    # The following options should print fewer URLs than the default.
    # If they print the same number of URLs, something is horribly broken.
    # If they say we missed 0 URLs, something is probably broken too.
    out = url('list', '--incorrect-name')
    incorrect_name_urls = len(out.split('\n'))
    assert 0 < incorrect_name_urls < total_urls

    out = url('list', '--incorrect-version')
    incorrect_version_urls = len(out.split('\n'))
    assert 0 < incorrect_version_urls < total_urls

    out = url('list', '--correct-name')
    correct_name_urls = len(out.split('\n'))
    assert 0 < correct_name_urls < total_urls

    out = url('list', '--correct-version')
    correct_version_urls = len(out.split('\n'))
    assert 0 < correct_version_urls < total_urls


@pytest.mark.network
def test_url_summary():
    """Test the URL summary command."""
    # test url_summary, the internal function that does the work
    (total_urls, correct_names, correct_versions,
     name_count_dict, version_count_dict) = url_summary(None)

    assert (0 < correct_names <=
            sum(name_count_dict.values()) <= total_urls)
    assert (0 < correct_versions <=
            sum(version_count_dict.values()) <= total_urls)

    # make sure it agrees with the actual command.
    out = url('summary')
    out_total_urls = int(
        re.search(r'Total URLs found:\s*(\d+)', out).group(1))
    assert out_total_urls == total_urls

    out_correct_names = int(
        re.search(r'Names correctly parsed:\s*(\d+)', out).group(1))
    assert out_correct_names == correct_names

    out_correct_versions = int(
        re.search(r'Versions correctly parsed:\s*(\d+)', out).group(1))
    assert out_correct_versions == correct_versions


def test_url_stats(capfd):
    with capfd.disabled():
        output = url('stats')
        npkgs = '%d packages' % len(spack.repo.all_package_names())
        assert npkgs in output
        assert 'total versions' in output