summaryrefslogtreecommitdiff
path: root/test/etap/010-file-basics.t
blob: 7c1c80ebb7331d8f95fb0104baead2decdd3a4fb (plain)
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
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -pa ./src/couchdb -sasl errlog_type error -boot start_sasl -noshell

filename() -> "./test/etap/temp.010".

main(_) ->
    code:add_pathz("src/couchdb"),
    etap:plan(16),
    case (catch test()) of
        ok ->
            etap:end_tests();
        Other ->
            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
            etap:bail()
    end,
    ok.
    
test() ->
    etap:is({error, enoent}, couch_file:open("not a real file"),
        "Opening a non-existant file should return an enoent error."),

    etap:fun_is(
        fun({ok, _}) -> true; (_) -> false end,
        couch_file:open(filename() ++ ".1", [create, invalid_option]),
        "Invalid flags to open are ignored."
    ),

    {ok, Fd} = couch_file:open(filename() ++ ".0", [create, overwrite]),
    etap:ok(is_pid(Fd),
        "Returned file descriptor is a Pid"),
    
    etap:is({ok, 0}, couch_file:bytes(Fd),
        "Newly created files have 0 bytes."),

    etap:is({ok, 0}, couch_file:append_term(Fd, foo),
        "Appending a term returns the previous end of file position."),

    {ok, Size} = couch_file:bytes(Fd),
    etap:is_greater(Size, 0,
        "Writing a term increased the file size."),
    
    etap:is({ok, Size}, couch_file:append_binary(Fd, <<"fancy!">>),
        "Appending a binary returns the current file size."),
    
    etap:is({ok, foo}, couch_file:pread_term(Fd, 0),
        "Reading the first term returns what we wrote: foo"),
    
    etap:is({ok, <<"fancy!">>}, couch_file:pread_binary(Fd, Size),
        "Reading back the binary returns what we wrote: <<\"fancy\">>."),
        
    etap:is({ok, <<131, 100, 0, 3, 102, 111, 111>>},
        couch_file:pread_binary(Fd, 0),
        "Reading a binary at a term position returns the term as binary."
    ),

    {ok, BinPos} = couch_file:append_binary(Fd, <<131,100,0,3,102,111,111>>),
    etap:is({ok, foo}, couch_file:pread_term(Fd, BinPos),
        "Reading a term from a written binary term representation succeeds."),

    % append_binary == append_iolist?
    % Possible bug in pread_iolist or iolist() -> append_binary
    {ok, IOLPos} = couch_file:append_binary(Fd, ["foo", $m, <<"bam">>]),
    etap:is({ok, [<<"foombam">>]}, couch_file:pread_iolist(Fd, IOLPos),
        "Reading an results in a binary form of the written iolist()"),

    % XXX: How does on test fsync?
    etap:is(ok, couch_file:sync(Fd),
        "Syncing does not cause an error."),

    etap:is(ok, couch_file:truncate(Fd, Size),
        "Truncating a file succeeds."),

    %etap:is(eof, (catch couch_file:pread_binary(Fd, Size)),
    %    "Reading data that was truncated fails.")
    etap:skip(fun() -> ok end,
        "No idea how to test reading beyond EOF"),
    
    etap:is({ok, foo}, couch_file:pread_term(Fd, 0),
        "Truncating does not affect data located before the truncation mark."),

    etap:is(ok, couch_file:close(Fd),
        "Files close properly."),
    ok.