diff mbox series

[bug#35929] tests: hackage: avoid mock, and extract test data

Message ID 20190527194556.59710-1-rob@vllmrt.net
State Accepted
Headers show
Series [bug#35929] tests: hackage: avoid mock, and extract test data | expand

Checks

Context Check Description
cbaines/applying patch fail Apply failed

Commit Message

Robert Vollmert May 27, 2019, 7:45 p.m. UTC
This is partially in order to make tests depend less on the
implementation of `hackage-fetch`, in preparation for hackage
revision import.

* tests/hackage.scm: Provide cabal input port directly, instead
of mocking hackage-fetch. And make cabal tests more explicit and
consistent by defining package pattern per test.
---
 tests/hackage.scm | 135 ++++++++++++++++++++++------------------------
 1 file changed, 63 insertions(+), 72 deletions(-)

Comments

Ludovic Courtès May 29, 2019, 9:16 p.m. UTC | #1
Hi Robert,

Robert Vollmert <rob@vllmrt.net> skribis:

> +(define* (eval-test-with-cabal test-cabal package-pattern #:key (cabal-environment '()))
> +  (define port (open-input-string test-cabal))
> +  (match (hackage->guix-package "foo" #:port port #:cabal-environment cabal-environment)
> +    (package-pattern #t)

This pattern matches anything and binds the result to ‘package-pattern’:

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> (let ((pattern '(a b)))
		       (match '(1 2 3) (pattern pattern)))
$9 = (1 2 3)
--8<---------------cut here---------------end--------------->8---

No more test failures!  :-)

The change you propose is a good idea but it needs to be implemented
using a macro:

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> (define-syntax-rule (match-pattern obj pattern)
		       (match obj
			 (pattern #t)
			 (_       #f)))
scheme@(guile-user)> (match-pattern '(1 2 3) (a b c))
$14 = #t
scheme@(guile-user)> (match-pattern '(1 2 3) (a b))
$15 = #f
scheme@(guile-user)> (match-pattern '(7 7) (a a))
$16 = #t
scheme@(guile-user)> (match-pattern '(1 2) (a a))
$17 = #f
--8<---------------cut here---------------end--------------->8---

HTH!

Ludo’.
Robert Vollmert May 29, 2019, 9:25 p.m. UTC | #2
> On 29. May 2019, at 23:16, Ludovic Courtès <ludo@gnu.org> wrote:
> Robert Vollmert <rob@vllmrt.net> skribis:
> 
>> +(define* (eval-test-with-cabal test-cabal package-pattern #:key (cabal-environment '()))
>> +  (define port (open-input-string test-cabal))
>> +  (match (hackage->guix-package "foo" #:port port #:cabal-environment cabal-environment)
>> +    (package-pattern #t)
> 
> This pattern matches anything and binds the result to ‘package-pattern’:
> 
> --8<---------------cut here---------------start------------->8---
> scheme@(guile-user)> (let ((pattern '(a b)))
> 		       (match '(1 2 3) (pattern pattern)))
> $9 = (1 2 3)
> --8<---------------cut here---------------end--------------->8---
> 
> No more test failures!  :-)
> 
> The change you propose is a good idea but it needs to be implemented
> using a macro:

Thanks, I’ll look into it. My first macro. :)
Robert Vollmert May 30, 2019, 11:42 a.m. UTC | #3
> On 29. May 2019, at 23:16, Ludovic Courtès <ludo@gnu.org> wrote:
> --8<---------------cut here---------------start------------->8---
> scheme@(guile-user)> (define-syntax-rule (match-pattern obj pattern)
> 		       (match obj
> 			 (pattern #t)
> 			 (_       #f)))
> scheme@(guile-user)> (match-pattern '(1 2 3) (a b c))
> $14 = #t
> scheme@(guile-user)> (match-pattern '(1 2 3) (a b))
> $15 = #f
> scheme@(guile-user)> (match-pattern '(7 7) (a a))
> $16 = #t
> scheme@(guile-user)> (match-pattern '(1 2) (a a))
> $17 = #f
> --8<---------------cut here---------------end--------------->8—

Hmm, I can’t quite get this to work. I’d like to supply a name for
the pattern, so I can reuse it across test cases, and I’m currently
at a loss for how to do tha:

scheme@(ice-9 match)> (define pattern '(a b c))
scheme@(ice-9 match)> (match-pattern '(a 2) pattern)
$30 = #t

This makes sense, because the pattern is the literal pattern, not whatever
pattern would evaluate to.

I’ve also tried things like

scheme@(ice-9 match)> (define-syntax-rule (pattern) (a b c))

thinking that the pattern definition should happen at macro level, too,
but can’t get that to work either… Do you have further hints?

…

I did manage to get things to work using eval, will send a revised
patch.

Thanks,
Robert
Ludovic Courtès May 31, 2019, 5:57 p.m. UTC | #4
Hi,

Robert Vollmert <rob@vllmrt.net> skribis:

>> On 29. May 2019, at 23:16, Ludovic Courtès <ludo@gnu.org> wrote:
>> --8<---------------cut here---------------start------------->8---
>> scheme@(guile-user)> (define-syntax-rule (match-pattern obj pattern)
>> 		       (match obj
>> 			 (pattern #t)
>> 			 (_       #f)))
>> scheme@(guile-user)> (match-pattern '(1 2 3) (a b c))
>> $14 = #t
>> scheme@(guile-user)> (match-pattern '(1 2 3) (a b))
>> $15 = #f
>> scheme@(guile-user)> (match-pattern '(7 7) (a a))
>> $16 = #t
>> scheme@(guile-user)> (match-pattern '(1 2) (a a))
>> $17 = #f
>> --8<---------------cut here---------------end--------------->8—
>
> Hmm, I can’t quite get this to work. I’d like to supply a name for
> the pattern, so I can reuse it across test cases, and I’m currently
> at a loss for how to do tha:
>
> scheme@(ice-9 match)> (define pattern '(a b c))
> scheme@(ice-9 match)> (match-pattern '(a 2) pattern)
> $30 = #t

Indeed, ‘match’ expects patterns to be literals.  So you cannot do:

  (define pattern …)

and then use that pattern in ‘match’ clauses.

If you wanted to do that, you would need to define a “procedural”
variant of ‘match’, which is not terribly difficult either, but it’s a
different beast.

> I did manage to get things to work using eval, will send a revised
> patch.

Please don’t use ‘eval’; there’s a saying that “eval is evil”, because
basically anything can happen when calling out to ‘eval’.

So as it turns out, I realize that the factorization you wanted to make
is not all that easy to achieve.  Perhaps adding a procedural pattern
matcher, which would be similar to ‘equal?’ except that it would ignore
the sha256, synopsis, and description, is the right way to go.

HTH!

Ludo’.
diff mbox series

Patch

diff --git a/tests/hackage.scm b/tests/hackage.scm
index 0efad0638d..a6d039afaa 100644
--- a/tests/hackage.scm
+++ b/tests/hackage.scm
@@ -155,93 +155,84 @@  library
 
 (test-begin "hackage")
 
-(define* (eval-test-with-cabal test-cabal #:key (cabal-environment '()))
-  (mock
-   ((guix import hackage) hackage-fetch
-    (lambda (name-version)
-      (call-with-input-string test-cabal
-        read-cabal)))
-   (match (hackage->guix-package "foo" #:cabal-environment cabal-environment)
-     (('package
-        ('name "ghc-foo")
-        ('version "1.0.0")
-        ('source
-         ('origin
-           ('method 'url-fetch)
-           ('uri ('string-append
-                  "https://hackage.haskell.org/package/foo/foo-"
-                  'version
-                  ".tar.gz"))
-           ('sha256
-            ('base32
-             (? string? hash)))))
-        ('build-system 'haskell-build-system)
-        ('inputs
-         ('quasiquote
-          (("ghc-http" ('unquote 'ghc-http))
-           ("ghc-mtl" ('unquote 'ghc-mtl)))))
-        ('home-page "http://test.org")
-        ('synopsis (? string?))
-        ('description (? string?))
-        ('license 'bsd-3))
-      #t)
-     (x
-      (pk 'fail x #f)))))
+(define ghc-foo-pattern
+ '('package
+    ('name "ghc-foo")
+    ('version "1.0.0")
+    ('source
+     ('origin
+      ('method 'url-fetch)
+       ('uri ('string-append
+              "https://hackage.haskell.org/package/foo/foo-"
+              'version
+              ".tar.gz"))
+       ('sha256
+        ('base32
+         (? string? hash)))))
+    ('build-system 'haskell-build-system)
+    ('inputs
+     ('quasiquote
+      (("ghc-http" ('unquote 'ghc-http))
+       ("ghc-mtl" ('unquote 'ghc-mtl)))))
+    ('home-page "http://test.org")
+    ('synopsis (? string?))
+    ('description (? string?))
+    ('license 'bsd-3)))
+
+(define* (eval-test-with-cabal test-cabal package-pattern #:key (cabal-environment '()))
+  (define port (open-input-string test-cabal))
+  (match (hackage->guix-package "foo" #:port port #:cabal-environment cabal-environment)
+    (package-pattern #t)
+    (x               (pk 'fail x #f))))
 
 (test-assert "hackage->guix-package test 1"
-  (eval-test-with-cabal test-cabal-1))
+  (eval-test-with-cabal test-cabal-1 ghc-foo-pattern))
 
 (test-assert "hackage->guix-package test 2"
-  (eval-test-with-cabal test-cabal-2))
+  (eval-test-with-cabal test-cabal-2 ghc-foo-pattern))
 
 (test-assert "hackage->guix-package test 3"
-  (eval-test-with-cabal test-cabal-3
+  (eval-test-with-cabal test-cabal-3 ghc-foo-pattern
                         #:cabal-environment '(("impl" . "ghc-7.8"))))
 
 (test-assert "hackage->guix-package test 4"
-  (eval-test-with-cabal test-cabal-4
+  (eval-test-with-cabal test-cabal-4 ghc-foo-pattern
                         #:cabal-environment '(("impl" . "ghc-7.8"))))
 
 (test-assert "hackage->guix-package test 5"
-  (eval-test-with-cabal test-cabal-5
+  (eval-test-with-cabal test-cabal-5 ghc-foo-pattern
                         #:cabal-environment '(("impl" . "ghc-7.8"))))
 
+(define ghc-foo-pattern-6
+ '('package
+    ('name "ghc-foo")
+    ('version "1.0.0")
+    ('source
+     ('origin
+      ('method 'url-fetch)
+       ('uri ('string-append
+              "https://hackage.haskell.org/package/foo/foo-"
+              'version
+              ".tar.gz"))
+       ('sha256
+        ('base32
+         (? string? hash)))))
+    ('build-system 'haskell-build-system)
+    ('inputs
+     ('quasiquote
+      (("ghc-b" ('unquote 'ghc-b))
+       ("ghc-http" ('unquote 'ghc-http))
+       ("ghc-mtl" ('unquote 'ghc-mtl)))))
+    ('native-inputs
+     ('quasiquote
+      (("ghc-haskell-gi" ('unquote 'ghc-haskell-gi)))))
+    ('home-page "http://test.org")
+    ('synopsis (? string?))
+    ('description (? string?))
+    ('license 'bsd-3)))
+
 (test-assert "hackage->guix-package test 6"
-  (mock
-   ((guix import hackage) hackage-fetch
-    (lambda (name-version)
-      (call-with-input-string test-cabal-6
-        read-cabal)))
-   (match (hackage->guix-package "foo")
-     (('package
-        ('name "ghc-foo")
-        ('version "1.0.0")
-        ('source
-         ('origin
-           ('method 'url-fetch)
-           ('uri ('string-append
-                  "https://hackage.haskell.org/package/foo/foo-"
-                  'version
-                  ".tar.gz"))
-           ('sha256
-            ('base32
-             (? string? hash)))))
-        ('build-system 'haskell-build-system)
-        ('inputs
-         ('quasiquote
-          (("ghc-b" ('unquote 'ghc-b))
-           ("ghc-http" ('unquote 'ghc-http))
-           ("ghc-mtl" ('unquote 'ghc-mtl)))))
-        ('native-inputs
-         ('quasiquote
-          (("ghc-haskell-gi" ('unquote 'ghc-haskell-gi)))))
-        ('home-page "http://test.org")
-        ('synopsis (? string?))
-        ('description (? string?))
-        ('license 'bsd-3))
-      #t)
-     (x
-      (pk 'fail x #f)))))
+  (eval-test-with-cabal test-cabal-6 ghc-foo-pattern-6))
 
 (test-assert "read-cabal test 1"
   (match (call-with-input-string test-read-cabal-1 read-cabal)