Add testing for spack blame; refactor llnl.util tests

This commit is contained in:
Todd Gamblin 2017-09-28 11:27:40 -07:00
parent 41a2652ef2
commit 8648e2cda5
7 changed files with 106 additions and 23 deletions

View file

@ -22,6 +22,8 @@
# License along with this program; if not, write to the Free Software # License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
############################################################################## ##############################################################################
from __future__ import division
import os import os
import re import re
import functools import functools
@ -104,21 +106,6 @@ def index_by(objects, *funcs):
return result return result
def partition_list(elements, predicate):
"""Partition a list into two lists, the first containing elements
for which the predicate evaluates to true, the second containing
those for which it is false.
"""
trues = []
falses = []
for elt in elements:
if predicate(elt):
trues.append(elt)
else:
falses.append(elt)
return trues, falses
def caller_locals(): def caller_locals():
"""This will return the locals of the *parent* of the caller. """This will return the locals of the *parent* of the caller.
This allows a function to insert variables into its caller's This allows a function to insert variables into its caller's
@ -425,30 +412,30 @@ def pretty_date(time, now=None):
if second_diff < 120: if second_diff < 120:
return "a minute ago" return "a minute ago"
if second_diff < 3600: if second_diff < 3600:
return str(second_diff / 60) + " minutes ago" return str(second_diff // 60) + " minutes ago"
if second_diff < 7200: if second_diff < 7200:
return "an hour ago" return "an hour ago"
if second_diff < 86400: if second_diff < 86400:
return str(second_diff / 3600) + " hours ago" return str(second_diff // 3600) + " hours ago"
if day_diff == 1: if day_diff == 1:
return "yesterday" return "yesterday"
if day_diff < 7: if day_diff < 7:
return str(day_diff) + " days ago" return str(day_diff) + " days ago"
if day_diff < 28: if day_diff < 28:
weeks = day_diff / 7 weeks = day_diff // 7
if weeks == 1: if weeks == 1:
return "a week ago" return "a week ago"
else: else:
return str(day_diff / 7) + " weeks ago" return str(day_diff // 7) + " weeks ago"
if day_diff < 365: if day_diff < 365:
months = day_diff / 30 months = day_diff // 30
if months == 1: if months == 1:
return "a month ago" return "a month ago"
elif months == 12: elif months == 12:
months -= 1 months -= 1
return str(months) + " months ago" return str(months) + " months ago"
diff = day_diff / 365 diff = day_diff // 365
if diff == 1: if diff == 1:
return "a year ago" return "a year ago"
else: else:

View file

@ -55,5 +55,5 @@ def test_blame_by_git(builtin_mock, capfd):
"""Sanity check the blame command to make sure it works.""" """Sanity check the blame command to make sure it works."""
with capfd.disabled(): with capfd.disabled():
out = blame('--git', 'mpich') out = blame('--git', 'mpich')
assert 'Mpich' in out assert 'class Mpich' in out
assert 'mock_packages' in out assert ' homepage = "http://www.mpich.org"' in out

View file

@ -0,0 +1,96 @@
##############################################################################
# Copyright (c) 2013-2017, 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/llnl/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 pytest
from datetime import datetime, timedelta
from llnl.util.lang import *
def test_pretty_date():
"""Make sure pretty_date prints the right dates."""
now = datetime.now()
just_now = now - timedelta(seconds=5)
assert pretty_date(just_now, now) == "just now"
seconds = now - timedelta(seconds=30)
assert pretty_date(seconds, now) == "30 seconds ago"
a_minute = now - timedelta(seconds=60)
assert pretty_date(a_minute, now) == "a minute ago"
minutes = now - timedelta(seconds=1800)
assert pretty_date(minutes, now) == "30 minutes ago"
an_hour = now - timedelta(hours=1)
assert pretty_date(an_hour, now) == "an hour ago"
hours = now - timedelta(hours=2)
assert pretty_date(hours, now) == "2 hours ago"
yesterday = now - timedelta(days=1)
assert pretty_date(yesterday, now) == "yesterday"
days = now - timedelta(days=3)
assert pretty_date(days, now) == "3 days ago"
a_week = now - timedelta(weeks=1)
assert pretty_date(a_week, now) == "a week ago"
weeks = now - timedelta(weeks=2)
assert pretty_date(weeks, now) == "2 weeks ago"
a_month = now - timedelta(days=30)
assert pretty_date(a_month, now) == "a month ago"
months = now - timedelta(days=60)
assert pretty_date(months, now) == "2 months ago"
a_year = now - timedelta(days=365)
assert pretty_date(a_year, now) == "a year ago"
years = now - timedelta(days=365 * 2)
assert pretty_date(years, now) == "2 years ago"
def test_match_predicate():
matcher = match_predicate(lambda x: True)
assert matcher('foo')
assert matcher('bar')
assert matcher('baz')
matcher = match_predicate(['foo', 'bar'])
assert matcher('foo')
assert matcher('bar')
assert not matcher('baz')
matcher = match_predicate(r'^(foo|bar)$')
assert matcher('foo')
assert matcher('bar')
assert not matcher('baz')
with pytest.raises(ValueError):
matcher = match_predicate(object())
matcher('foo')