Skip to content

Commit

Permalink
ValidatePackageInfo accepts date format yyyy-mm-dd
Browse files Browse the repository at this point in the history
If the date format is dd/mm/yyyy and InfoLevel InfoPackageLoading
is set to at least 2 it prints a hint to change the format.

Add validity check for dates

Update relevant testfile

Fixes gap-system#2727
  • Loading branch information
Lucas Wollenhaupt authored and wucas committed Mar 22, 2019
1 parent 38aa4e8 commit e5a30d6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
42 changes: 38 additions & 4 deletions lib/package.gi
Original file line number Diff line number Diff line change
Expand Up @@ -2135,7 +2135,7 @@ InstallGlobalFunction( DeclareAutoreadableVariables,
InstallGlobalFunction( ValidatePackageInfo, function( info )
local record, pkgdir, i, IsStringList, IsRecordList, IsProperBool, IsURL,
IsFilename, IsFilenameList, result, TestOption, TestMandat, subrec,
list;
list, CheckDateValidity;

if IsString( info ) then
if IsReadableFile( info ) then
Expand Down Expand Up @@ -2203,9 +2203,43 @@ InstallGlobalFunction( ValidatePackageInfo, function( info )
x -> IsString(x) and 0 < Length(x) and x[1] <> '=',
"a nonempty string that does not start with `='" );
TestMandat( record, "Date",
x -> IsString(x) and Length(x) = 10 and x{ [3,6] } = "//"
and ForAll( x{ [1,2,4,5,7,8,9,10] }, IsDigitChar ),
"a string of the form `dd/mm/yyyy'" );
x -> IsString(x) and Length(x) = 10
and ( ( x{ [3,6] } = "//" and ForAll( x{ [1,2,4,5,7,8,9,10] }, IsDigitChar ) )
or ( x{ [5,8] } = "--" and ForAll( x{ [1,2,3,4,6,7,9,10] }, IsDigitChar ) ) ) ,
"a string of the form `yyyy-mm-dd` or `dd/mm/yyyy`" );

#If the date is in the format `dd/mm/yyyy` a message is printed
if IsBound( record.Date ) and record.Date{ [3,6] } = "//" then
Info( InfoPackageLoading, 2, Concatenation(record.PackageName, ": Please be adviced to change the date format to `yyyy-mm-dd`"));
fi;

#check if the date is valid
CheckDateValidity := function(x)
local date;
if x{ [3,6] } = "//" then #for the old format split at '/'
date := List( SplitString( x, "/" ), Int);
#the format dd/mm/yyyy is ambigous and can be confused with mm/dd/yyyy
#if it is clear from the date that the format is mm/dd/yyyy print a message
if date[2] in [13..31] and date[1] in [1..12] then
TestMandat( record, "Date",
x -> false, "a string of the form `yyyy-mm-dd` or `dd/mm/yyyy`" );
return true;
fi;
elif x{ [5,8] } = "--" then #for the yyyy-mm-dd format split at '-'
date := List( SplitString( date, "-" ), Int);
Permuted(date, (1,3)); #sort such that date=[dd,mm,yyyy]
fi;
if IsBound(date) and (not (date[1] in [1..31] and
date[2] in [1..12] and
date[3] > 1999 and # GAP 4 appeared in 1999
date[1] <= DaysInMonth( date[2], date[3] ))) then
return false;
fi;
return true;
end;
TestMandat( record, "Date",
x -> IsString(x) and CheckDateValidity(x), "a valid date");

TestOption( record, "License",
x -> IsString(x) and 0 < Length(x),
"a nonempty string containing an SPDX ID" );
Expand Down
33 changes: 32 additions & 1 deletion tst/testinstall/package.tst
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ gap> ValidatePackageInfo(rec());
#E component `Subtitle' must be bound to a string
#E component `Version' must be bound to a nonempty string that does not start\
with `='
#E component `Date' must be bound to a string of the form `dd/mm/yyyy'
#E component `Date' must be bound to a string of the form `yyyy-mm-dd` or `dd\
/mm/yyyy`
#E component `Date' must be bound to a valid date
#E component `ArchiveURL' must be bound to a string started with http://, htt\
ps:// or ftp://
#E component `ArchiveFormats' must be bound to a string
Expand All @@ -222,6 +224,35 @@ gap> info := rec(
> PackageName := "pkg",
> Subtitle := "desc",
> Version := "0",
> Date := "01/20/2015",
> ArchiveURL := "https://",
> ArchiveFormats := "",
> Status := "other",
> README_URL := "https://",
> PackageInfoURL := "https://",
> AbstractHTML := "",
> PackageWWWHome := "https://",
> PackageDoc := rec(),
> AvailabilityTest := ReturnTrue,
> );;
gap> ValidatePackageInfo(info);
#E component `Date' must be bound to a string of the form `yyyy-mm-dd` or `dd\
/mm/yyyy`
#E component `BookName' must be bound to a string
#E component `ArchiveURLSubset' must be bound to a list of strings denoting r\
elative paths to readable files or directories
#E component `HTMLStart' must be bound to a string denoting a relative path t\
o a readable file
#E component `PDFFile' must be bound to a string denoting a relative path to \
a readable file
#E component `SixFile' must be bound to a string denoting a relative path to \
a readable file
#E component `LongTitle' must be bound to a string
false
gap> info := rec(
> PackageName := "pkg",
> Subtitle := "desc",
> Version := "0",
> Date := "01/02/3000",
> ArchiveURL := "https://",
> ArchiveFormats := "",
Expand Down

0 comments on commit e5a30d6

Please sign in to comment.