diff --git a/+stdlib/+fileio/md5sum.m b/+stdlib/+fileio/md5sum.m index 960ccce..adfdefb 100644 --- a/+stdlib/+fileio/md5sum.m +++ b/+stdlib/+fileio/md5sum.m @@ -9,8 +9,6 @@ file = expanduser(file); assert(isfile(file), '%s not found', file) -hash = string.empty; - if ismac [stat,hash] = system("md5 -r " + file); elseif isunix @@ -18,14 +16,13 @@ elseif ispc [stat,hash] = system("CertUtil -hashfile " + file + " MD5"); else - return + error("no sha256sum method for your OS") end -if stat ~= 0 - return -end +assert(stat == 0, hash) hash = regexp(hash,'^\w{32}','match','once','lineanchors'); +hash = string(hash); assert(strlength(hash)==32, 'md5 hash is 32 characters') diff --git a/+stdlib/+fileio/sha256sum.m b/+stdlib/+fileio/sha256sum.m index 7e4d954..c8956f4 100644 --- a/+stdlib/+fileio/sha256sum.m +++ b/+stdlib/+fileio/sha256sum.m @@ -9,8 +9,6 @@ file = expanduser(file); assert(isfile(file), '%s not found', file) -hash = string.empty; - if ismac [stat,hash] = system("shasum --algorithm 256 --binary " + file); elseif isunix @@ -18,14 +16,13 @@ elseif ispc [stat,hash] = system("CertUtil -hashfile " + file + " SHA256"); else - return + error("no md5sum method for your OS") end -if stat ~= 0 - return -end +assert(stat == 0, hash) hash = regexp(hash,'^\w{64}','match','once','lineanchors'); +hash = string(hash); assert(strlength(hash)==64, 'SHA256 hash is 64 characters') diff --git a/+stdlib/TestFileio.m b/+stdlib/TestFileio.m index 2e10fcd..9572001 100644 --- a/+stdlib/TestFileio.m +++ b/+stdlib/TestFileio.m @@ -142,6 +142,21 @@ function test_copyfile(tc) tc.verifyThat(fullfile(tempdir, name), IsFile) end +function test_hash(tc) +import matlab.unittest.constraints.IsFile + +fn = tempname; +fid = fopen(fn, "w"); +tc.assumeGreaterThan(fid, 0); +fprintf(fid, "hello"); +fclose(fid); +tc.assumeThat(fn, IsFile) + +tc.verifyEqual(stdlib.fileio.md5sum(fn), "5d41402abc4b2a76b9719d911017c592") +tc.verifyEqual(stdlib.fileio.sha256sum(fn), "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824") + +delete(fn) end end +end diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 90fe32b..976c38a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,7 +8,8 @@ on: jobs: - linux: + + matlab: timeout-minutes: 15 runs-on: ${{ matrix.os }}