From 8648e2cda55cbc84cac4cc6316f3f51f9b38ba7d Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Thu, 28 Sep 2017 11:27:40 -0700 Subject: [PATCH] Add testing for spack blame; refactor llnl.util tests --- lib/spack/llnl/util/lang.py | 29 ++---- lib/spack/spack/test/cmd/blame.py | 4 +- .../spack/test/{ => llnl/util}/file_list.py | 0 lib/spack/spack/test/llnl/util/lang.py | 96 +++++++++++++++++++ .../spack/test/{ => llnl/util}/link_tree.py | 0 lib/spack/spack/test/{ => llnl/util}/lock.py | 0 lib/spack/spack/test/{ => llnl/util}/log.py | 0 7 files changed, 106 insertions(+), 23 deletions(-) rename lib/spack/spack/test/{ => llnl/util}/file_list.py (100%) create mode 100644 lib/spack/spack/test/llnl/util/lang.py rename lib/spack/spack/test/{ => llnl/util}/link_tree.py (100%) rename lib/spack/spack/test/{ => llnl/util}/lock.py (100%) rename lib/spack/spack/test/{ => llnl/util}/log.py (100%) diff --git a/lib/spack/llnl/util/lang.py b/lib/spack/llnl/util/lang.py index 07338b21ef..00e6c10b3f 100644 --- a/lib/spack/llnl/util/lang.py +++ b/lib/spack/llnl/util/lang.py @@ -22,6 +22,8 @@ # License along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ############################################################################## +from __future__ import division + import os import re import functools @@ -104,21 +106,6 @@ def index_by(objects, *funcs): 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(): """This will return the locals of the *parent* of the caller. 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: return "a minute ago" if second_diff < 3600: - return str(second_diff / 60) + " minutes ago" + return str(second_diff // 60) + " minutes ago" if second_diff < 7200: return "an hour ago" if second_diff < 86400: - return str(second_diff / 3600) + " hours ago" + return str(second_diff // 3600) + " hours ago" if day_diff == 1: return "yesterday" if day_diff < 7: return str(day_diff) + " days ago" if day_diff < 28: - weeks = day_diff / 7 + weeks = day_diff // 7 if weeks == 1: return "a week ago" else: - return str(day_diff / 7) + " weeks ago" + return str(day_diff // 7) + " weeks ago" if day_diff < 365: - months = day_diff / 30 + months = day_diff // 30 if months == 1: return "a month ago" elif months == 12: months -= 1 return str(months) + " months ago" - diff = day_diff / 365 + diff = day_diff // 365 if diff == 1: return "a year ago" else: diff --git a/lib/spack/spack/test/cmd/blame.py b/lib/spack/spack/test/cmd/blame.py index 0031052b24..9342ff1f7c 100644 --- a/lib/spack/spack/test/cmd/blame.py +++ b/lib/spack/spack/test/cmd/blame.py @@ -55,5 +55,5 @@ def test_blame_by_git(builtin_mock, capfd): """Sanity check the blame command to make sure it works.""" with capfd.disabled(): out = blame('--git', 'mpich') - assert 'Mpich' in out - assert 'mock_packages' in out + assert 'class Mpich' in out + assert ' homepage = "http://www.mpich.org"' in out diff --git a/lib/spack/spack/test/file_list.py b/lib/spack/spack/test/llnl/util/file_list.py similarity index 100% rename from lib/spack/spack/test/file_list.py rename to lib/spack/spack/test/llnl/util/file_list.py diff --git a/lib/spack/spack/test/llnl/util/lang.py b/lib/spack/spack/test/llnl/util/lang.py new file mode 100644 index 0000000000..133ef6cc6c --- /dev/null +++ b/lib/spack/spack/test/llnl/util/lang.py @@ -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') diff --git a/lib/spack/spack/test/link_tree.py b/lib/spack/spack/test/llnl/util/link_tree.py similarity index 100% rename from lib/spack/spack/test/link_tree.py rename to lib/spack/spack/test/llnl/util/link_tree.py diff --git a/lib/spack/spack/test/lock.py b/lib/spack/spack/test/llnl/util/lock.py similarity index 100% rename from lib/spack/spack/test/lock.py rename to lib/spack/spack/test/llnl/util/lock.py diff --git a/lib/spack/spack/test/log.py b/lib/spack/spack/test/llnl/util/log.py similarity index 100% rename from lib/spack/spack/test/log.py rename to lib/spack/spack/test/llnl/util/log.py