]> git.ipfire.org Git - thirdparty/git.git/blame - reftable/refname_test.c
split-index: accept that a base index can be empty
[thirdparty/git.git] / reftable / refname_test.c
CommitLineData
acb53344
HWN
1/*
2Copyright 2020 Google LLC
3
4Use of this source code is governed by a BSD-style
5license that can be found in the LICENSE file or at
6https://developers.google.com/open-source/licenses/bsd
7*/
8
9#include "basics.h"
10#include "block.h"
11#include "blocksource.h"
12#include "constants.h"
13#include "reader.h"
14#include "record.h"
15#include "refname.h"
16#include "reftable-error.h"
17#include "reftable-writer.h"
18#include "system.h"
19
20#include "test_framework.h"
21#include "reftable-tests.h"
22
23struct testcase {
24 char *add;
25 char *del;
26 int error_code;
27};
28
29static void test_conflict(void)
30{
31 struct reftable_write_options opts = { 0 };
32 struct strbuf buf = STRBUF_INIT;
33 struct reftable_writer *w =
34 reftable_new_writer(&strbuf_add_void, &buf, &opts);
35 struct reftable_ref_record rec = {
36 .refname = "a/b",
37 .value_type = REFTABLE_REF_SYMREF,
38 .value.symref = "destination", /* make sure it's not a symref.
39 */
40 .update_index = 1,
41 };
42 int err;
43 int i;
44 struct reftable_block_source source = { NULL };
45 struct reftable_reader *rd = NULL;
46 struct reftable_table tab = { NULL };
47 struct testcase cases[] = {
48 { "a/b/c", NULL, REFTABLE_NAME_CONFLICT },
49 { "b", NULL, 0 },
50 { "a", NULL, REFTABLE_NAME_CONFLICT },
51 { "a", "a/b", 0 },
52
53 { "p/", NULL, REFTABLE_REFNAME_ERROR },
54 { "p//q", NULL, REFTABLE_REFNAME_ERROR },
55 { "p/./q", NULL, REFTABLE_REFNAME_ERROR },
56 { "p/../q", NULL, REFTABLE_REFNAME_ERROR },
57
58 { "a/b/c", "a/b", 0 },
59 { NULL, "a//b", 0 },
60 };
61 reftable_writer_set_limits(w, 1, 1);
62
63 err = reftable_writer_add_ref(w, &rec);
64 EXPECT_ERR(err);
65
66 err = reftable_writer_close(w);
67 EXPECT_ERR(err);
68 reftable_writer_free(w);
69
70 block_source_from_strbuf(&source, &buf);
71 err = reftable_new_reader(&rd, &source, "filename");
72 EXPECT_ERR(err);
73
74 reftable_table_from_reader(&tab, rd);
75
76 for (i = 0; i < ARRAY_SIZE(cases); i++) {
77 struct modification mod = {
78 .tab = tab,
79 };
80
81 if (cases[i].add) {
82 mod.add = &cases[i].add;
83 mod.add_len = 1;
84 }
85 if (cases[i].del) {
86 mod.del = &cases[i].del;
87 mod.del_len = 1;
88 }
89
90 err = modification_validate(&mod);
91 EXPECT(err == cases[i].error_code);
92 }
93
94 reftable_reader_free(rd);
95 strbuf_release(&buf);
96}
97
98int refname_test_main(int argc, const char *argv[])
99{
100 RUN_TEST(test_conflict);
101 return 0;
102}