forked from philhofer/distill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecline-test.scm
101 lines (89 loc) · 2.42 KB
/
execline-test.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
(cond-expand
(csi (include "execline.mod.scm")
(include "kvector.mod.scm")
(include "text.mod.scm"))
(else (begin)))
(include "test-helpers.scm")
(import
scheme
(chicken port)
(distill kvector)
(distill execline))
(define (exexpr->string expr)
(with-output-to-string
(lambda () (write-exexpr expr))))
(define conf "/etc/sysctl.conf")
(define
script
`(if (sed -e s/foo/bar/g "s/foo = bar/bar = foo/g" file)
ifelse (echo #u8(127 69 76 70)) (echo "echo failed")
if (sysctl -p ,conf)
;; test (lack of) quoting for simple strings
forbacktickx file (pipeline
(elglob extra "/etc/sysctl.d/*.conf" echo $extra)
sort)
importas file f
echo $f))
(define
want
#<<EOF
#!/bin/execlineb -P
if {
sed -e s/foo/bar/g "s/foo = bar/bar = foo/g" file
}
ifelse {
echo "\0x7fELF"
} {
echo "echo failed"
}
if {
sysctl -p /etc/sysctl.conf
}
forbacktickx file {
pipeline {
elglob extra /etc/sysctl.d/*.conf echo $extra
} sort
}
importas file f echo $f
EOF
)
(define scriptout (exexpr->string script))
(when (not (equal? scriptout want))
(display "---- script: ----\n")
(display scriptout)
(display "---- want: ----\n")
(display want)
(error "scripts not equal"))
(test equal?
'(if (./configure x y z)
if (make args)
make install)
(elif*
'(./configure x y z)
'(make args)
'(make install)))
(test string=?
"CFLAGS=-ffoo -fbar"
((el= 'CFLAGS= identity) '(-ffoo -fbar)))
;; simple template substitution test
(let* (($CC identity)
($CFLAGS (lambda (cc) '(-ffoo -fbar)))
(template `(if (make ,(elconc 'CC= $CC)
,(el= 'CFLAGS= $CFLAGS))
make install)))
(test equal?
'(if (make "CC=gcc" "CFLAGS=-ffoo -fbar")
make install)
(elexpand "gcc" template)))
;; test optimization: sublists that are not
;; altered can be returned as themselves
(let* ((template '(make DESTDIR=/out install)))
(test eq? template (elexpand #f template)))
;; test expansion within elif
(let* (($var identity)
($subscript (lambda (conf)
(elexpand conf `(redirfd -r /dev/urandom if (head -c20) echo ,$var))))
(topscript (elif `(echo ,$var) (list $subscript))))
(test equal?
'(if (echo "hi") redirfd -r /dev/urandom if (head -c20) echo "hi")
(elexpand "hi" topscript)))