summaryrefslogtreecommitdiff
path: root/subversion/tests/libsvn_subr/target-test.c
blob: 9ab42c13c38513aa64602dca4cf463935d92d898 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * target-test.c: test the condense_targets functions
 *
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 */

#include <apr_general.h>


#ifdef _MSC_VER
#include <direct.h>
#define getcwd _getcwd
#else
#include <unistd.h> /* for getcwd() */
#endif

#define SVN_DEPRECATED

#include "svn_pools.h"
#include "svn_path.h"

#include "../svn_test.h"

typedef svn_error_t *(*condense_targets_func_t)
                           (const char **pcommon,
                            apr_array_header_t **pcondensed_targets,
                            const apr_array_header_t *targets,
                            svn_boolean_t remove_redundancies,
                            apr_pool_t *pool);

/** Executes function CONDENSE_TARGETS twice - with and without requesting the
 * condensed targets list -  on TEST_TARGETS (comma sep. string) and compares
 * the results with EXP_COMMON and EXP_TARGETS (comma sep. string).
 *
 * @note: a '%' character at the beginning of EXP_COMMON or EXP_TARGETS will
 * be replaced by the current working directory.
 *
 * Returns an error if any of the comparisons fail.
 */
static svn_error_t *
condense_targets_tests_helper(const char* title,
                              const char* test_targets,
                              const char* exp_common,
                              const char* exp_targets,
                              const char* func_name,
                              condense_targets_func_t condense_targets,
                              apr_pool_t *pool)
{
  apr_array_header_t *targets;
  apr_array_header_t *condensed_targets;
  const char *common_path, *common_path2, *curdir;
  char *token, *iter;
  const char *exp_common_abs = exp_common;
  int i;
  char buf[8192];

  if (! getcwd(buf, sizeof(buf)))
    return svn_error_create(SVN_ERR_BASE, NULL, "getcwd() failed");
  curdir = svn_path_internal_style(buf, pool);

  /* Create the target array */
  targets = apr_array_make(pool, sizeof(test_targets), sizeof(const char *));
  token = apr_strtok(apr_pstrdup(pool, test_targets), ",", &iter);
  while (token)
    {
      APR_ARRAY_PUSH(targets, const char *) =
        svn_path_internal_style(token, pool);
      token = apr_strtok(NULL, ",", &iter);
    };

  /* Call the function */
  SVN_ERR(condense_targets(&common_path, &condensed_targets, targets,
                           TRUE, pool));

  /* Verify the common part with the expected (prefix with cwd). */
  if (*exp_common == '%')
    exp_common_abs = apr_pstrcat(pool, curdir, exp_common + 1, (char *)NULL);

  if (strcmp(common_path, exp_common_abs) != 0)
    {
      return svn_error_createf
        (SVN_ERR_TEST_FAILED, NULL,
         "%s (test %s) returned %s instead of %s",
           func_name, title,
           common_path, exp_common_abs);
    }

  /* Verify the condensed targets */
  token = apr_strtok(apr_pstrdup(pool, exp_targets), ",", &iter);
  for (i = 0; i < condensed_targets->nelts; i++)
    {
      const char * target = APR_ARRAY_IDX(condensed_targets, i, const char*);
      if (token && (*token == '%'))
        token = apr_pstrcat(pool, curdir, token + 1, (char *)NULL);
      if (! token ||
          (target && (strcmp(target, token) != 0)))
        {
          return svn_error_createf
            (SVN_ERR_TEST_FAILED, NULL,
             "%s (test %s) couldn't find %s in expected targets list",
               func_name, title,
               target);
        }
      token = apr_strtok(NULL, ",", &iter);
    }

  /* Now ensure it works without the pbasename */
  SVN_ERR(condense_targets(&common_path2, NULL, targets, TRUE, pool));

  /* Verify the common part again */
  if (strcmp(common_path, common_path2) != 0)
    {
      return svn_error_createf
        (SVN_ERR_TEST_FAILED, NULL,
         "%s (test %s): Common path without getting targets %s does not match" \
         "common path with targets %s",
          func_name, title,
          common_path2, common_path);
    }

  return SVN_NO_ERROR;
}


static svn_error_t *
test_path_condense_targets(apr_pool_t *pool)
{
  int i;
  struct {
    const char* title;
    const char* targets;
    const char* exp_common;
    const char* exp_targets;
  } tests[] = {
    { "normal use", "z/A/B,z/A,z/A/C,z/D/E,z/D/F,z/D,z/G,z/G/H,z/G/I",
      "%/z", "A,D,G" },
    {"identical dirs", "z/A,z/A,z/A,z/A",
     "%/z/A", "" },
    {"identical files", "z/A/file,z/A/file,z/A/file,z/A/file",
     "%/z/A/file", "" },
    {"single dir", "z/A",
     "%/z/A", "" },
    {"single file", "z/A/file",
     "%/z/A/file", "" },
    {"URLs", "http://host/A/C,http://host/A/C/D,http://host/A/B/D",
     "http://host/A", "C,B/D" },
    {"URLs with no common prefix",
     "http://host1/A/C,http://host2/A/C/D,http://host3/A/B/D",
     "", "http://host1/A/C,http://host2/A/C/D,http://host3/A/B/D" },
    {"file URLs with no common prefix", "file:///A/C,file:///B/D",
     "", "file:///A/C,file:///B/D" },
    {"URLs with mixed protocols",
     "http://host/A/C,file:///B/D,gopher://host/A",
     "", "http://host/A/C,file:///B/D,gopher://host/A" },
    {"mixed paths and URLs",
     "z/A/B,z/A,http://host/A/C/D,http://host/A/C",
     "", "%/z/A,http://host/A/C" },
  };

  for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
    {
      SVN_ERR(condense_targets_tests_helper(tests[i].title,
                                            tests[i].targets,
                                            tests[i].exp_common,
                                            tests[i].exp_targets,
                                            "svn_path_condense_targets",
                                            svn_path_condense_targets,
                                            pool));
    }

  return SVN_NO_ERROR;
}



/* The test table.  */

struct svn_test_descriptor_t test_funcs[] =
  {
    SVN_TEST_NULL,
    SVN_TEST_PASS2(test_path_condense_targets,
                   "test svn_path_condense_targets"),
    SVN_TEST_NULL
  };