Skip to content

Commit

Permalink
feat: ensure temp files are removed
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Feb 6, 2025
1 parent 38b7bd1 commit 8774b4a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-stingrays-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"api-tuner": patch
---

When request fails, ensure that the temporary files are cleaned-up
2 changes: 1 addition & 1 deletion rules/files.n3
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ prefix file: <http://www.w3.org/2000/10/swap/file#>
{
?path file:rm ?iDoNotCare .
} <= {
( "rm " ?path )!string:concatenation!e:exec .
( "rm " ?path " 2> /dev/null" )!string:concatenation!e:exec .
} .

{
Expand Down
60 changes: 32 additions & 28 deletions rules/requests.n3
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,41 @@ prefix earl: <http://www.w3.org/ns/earl#>
( ?responseBodyFile ".curl.json" ) string:concatenation ?responseHeadersFile .
( ?responseBodyFile ".n3" ) string:concatenation ?responseFile .

( "Calling " ?method " " ?endpoint )!string:concatenation^tuner:info .

(
{ ?req tuner:body ?body }
{ ?body </#body> ( ?bodyArgs ?requestBodyFile ) }
{ ?bodyArgs log:equalTo "" }
) log:ifThenElseIn [] .
{
( "Calling " ?method " " ?endpoint )!string:concatenation^tuner:info .

(
"curl -s -X " ?method " '" ?endpoint "'"
?headersArgs
?bodyArgs
" -w @" ( "lib/curl-format.txt"!file:libPath )!string:concatenation
" -o " ?responseBodyFile
" > " ?responseHeadersFile
) string:concatenation ?command .
(
{ ?req tuner:body ?body }
{ ?body </#body> ( ?bodyArgs ?requestBodyFile ) }
{ ?bodyArgs log:equalTo "" }
) log:ifThenElseIn [] .

?command^tuner:trace .
?command!e:exec .
(
"curl -s -X " ?method " '" ?endpoint "'"
?headersArgs
?bodyArgs
" -w @" ( "lib/curl-format.txt"!file:libPath )!string:concatenation
" -o " ?responseBodyFile
" > " ?responseHeadersFile
) string:concatenation ?command .

(
"node " "lib/merge-curl-output.js"!file:libPath " "
?responseBodyFile
" > " ?responseFile
)!string:concatenation!e:exec .
?command^tuner:trace .
?command!e:exec .

("file://" ?responseFile)!string:concatenation^log:uri log:semantics ?res .
?res^tuner:trace .
(
"node " "lib/merge-curl-output.js"!file:libPath " "
?responseBodyFile
" > " ?responseFile
)!string:concatenation!e:exec .

( { ?requestBodyFile log:rawType log:Literal } { ?requestBodyFile!file:rm } true ) log:ifThenElseIn [] .
?responseHeadersFile!file:rm .
?responseBodyFile!file:rm .
?responseFile!file:rm .
("file://" ?responseFile)!string:concatenation^log:uri log:semantics ?res .
?res^tuner:trace .
} log:callWithCleanup {
{
( { ?requestBodyFile log:rawType log:Literal } { ?requestBodyFile!file:rm } true ) log:ifThenElseIn [] .
} log:callWithCleanup true .
{ ?responseHeadersFile!file:rm } log:callWithCleanup true .
{ ?responseBodyFile!file:rm } log:callWithCleanup true .
{ ?responseFile!file:rm } log:callWithCleanup true .
} .
} .
32 changes: 32 additions & 0 deletions tests/failed-requests.n3
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
prefix : <https://api-tuner.described.at/test#http-code/>
PREFIX earl: <http://www.w3.org/ns/earl#>
PREFIX tuner: <https://api-tuner.described.at/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX log: <http://www.w3.org/2000/10/swap/log#>
PREFIX string: <http://www.w3.org/2000/10/swap/string#>
prefix e: <http://eulersharp.sourceforge.net/2003/03swap/log-rules#>

:
a earl:TestCase ;
rdfs:label "Test dailed request" ;
tuner:request [
a tuner:Request ;
tuner:url <http://foo.bar/baz> ;
tuner:method "GET" ;
]
.

{
(
{ :!tuner:request tuner:response ?res }
{
"Expected failure"^tuner:info .
true log:equalTo false .
}
{
"if [[ `git status --porcelain` ]]; then exit 1; fi" e:exec 0 .
}
) log:ifThenElseIn [] .
} => {
: earl:outcome earl:passed .
} .

0 comments on commit 8774b4a

Please sign in to comment.