nspkg / tests / retrievalfixtures / fixtures.v
35 lines · 30 sloc · 685 bytes · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module retrievalfixtures
2
3import os
4
5pub struct Reply {
6pub:
7 status int = -1
8 data []u8
9}
10
11pub struct Call {
12pub:
13 url string
14 filename string
15 force bool
16 trailing_slash bool
17}
18
19pub struct Plan {
20pub mut:
21 replies map[string]Reply
22 calls []Call
23}
24
25pub fn (mut plan Plan) fetch(url string, directory string, filename string, force bool) int {
26 plan.calls << Call{ url: url, filename: filename, force: force, trailing_slash: directory.ends_with('/') }
27 reply := plan.replies[url] or { return -1 }
28 if reply.status == 0 {
29 os.write_file_array(os.join_path(directory, filename), reply.data) or {
30 eprintln(err)
31 return -1
32 }
33 }
34 return reply.status
35}
36