source: branches/0.8-stable/test/parameter_test.cc @ 1250

Last change on this file since 1250 was 1250, checked in by Peter Johansson, 10 years ago

adding missing include

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1// $Id: parameter_test.cc 1250 2010-10-31 14:11:20Z peter $
2
3/*
4  Copyright (C) 2010 Peter Johansson
5
6  This file is part of svndigest, http://dev.thep.lu.se/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12
13  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "Suite.h"
23#include "environment.h"
24
25#include "bin/svndigestParameter.h"
26#include "lib/utility.h"
27
28#include "yat/Exception.h"
29
30#include <cassert>
31#include <cerrno>
32#include <cstring>
33#include <string>
34#include <vector>
35
36using namespace theplu::svndigest;
37
38void test_root(test::Suite& suite);
39void test_root(test::Suite& suite, const std::string& arg, 
40               const std::string& root, const std::string& root_basename);
41void parse(Parameter&, const std::vector<std::string>& argv);
42
43int main(int argc, char* argv[])
44{
45  test::Suite suite(argc, argv);
46  test_root(suite);
47  if (suite.ok())
48    return 0;
49  return 1;
50}
51
52
53void test_root(test::Suite& suite)
54{
55  suite.out() << "test --root ROOT\n";
56  test_root(suite, ".", test::abs_builddir()+"/testSubDir/parameter_test",
57            "parameter_test");
58  test_root(suite, "..", test::abs_builddir()+"/testSubDir","testSubDir");
59  test_root(suite, "../..", test::abs_builddir(), "test");
60  // test with absolute path
61  test_root(suite, test::abs_builddir(), test::abs_builddir(), "test");
62  // test with symbolic link
63  if (!node_exist("symlink")) {
64    if (symlink(".", "symlink")) {
65      suite.add(false);
66      suite.out() << "creating symlink failed: " << strerror(errno) << "\n";
67    }
68  }
69  test_root(suite, "symlink", 
70            test::abs_builddir()+"/testSubDir/parameter_test", "symlink");
71  test_root(suite, "symlink/.", 
72            test::abs_builddir()+"/testSubDir/parameter_test","parameter_test");
73  test_root(suite, "symlink/..", 
74            test::abs_builddir()+"/testSubDir", "testSubDir");
75  test_root(suite, test::abs_builddir()+"/testSubDir/parameter_test/symlink/..", 
76            test::abs_builddir()+"/testSubDir", "testSubDir");
77  test_root(suite, test::abs_builddir()+"/testSubDir/parameter_test/symlink", 
78            test::abs_builddir()+"/testSubDir/parameter_test", "symlink");
79 
80  try {
81    test_root(suite, "../../Makefile", test::abs_builddir()+"/Makefile", 
82              "Makefile");
83    suite.add(false);
84    suite.out() << "error: no exception thrown\n --root ../../Makefile\n";
85  }
86  catch (theplu::yat::utility::cmd_error& e) {
87    suite.out() << "caught expected errno_error: what(): " << e.what();
88  }
89}
90
91void test_root(test::Suite& suite, const std::string& arg, 
92               const std::string& root, const std::string& root_basename)
93{
94  svndigestParameter option;
95  std::vector<std::string> argv;
96  argv.push_back("svndigest");
97  argv.push_back("--no-report");
98  argv.push_back("--root");
99  argv.push_back(arg);
100  parse(option, argv);
101  if (option.root() != root) {
102    suite.add(false);
103    for (size_t i=0; i<argv.size(); ++i)
104      suite.out() << argv[i] << " ";
105    suite.out() << "\n";
106    suite.out() << "root: `" << option.root() << "'\n";
107    suite.out() << "expected: `" << root << "'\n";
108  }
109  if (option.root_basename() != root_basename) {
110    suite.add(false);
111    for (size_t i=0; i<argv.size(); ++i)
112      suite.out() << argv[i] << " ";
113    suite.out() << "\n";
114    suite.out() << "root: `" << option.root_basename() << "'\n";
115    suite.out() << "expected: `" << root_basename << "'\n";
116  }
117}
118
119
120void parse(Parameter& option, const std::vector<std::string>& vec)
121{
122  int argc = vec.size();
123  char** argv = new char*[vec.size()];
124  for (int i = 0; i<argc; ++i) {
125    argv[i] = strdup(vec[i].c_str());
126    assert(argv[i]);
127  }
128  option.parse(argc, argv);
129  for (int i = 0; i<argc; ++i) {
130    free(argv[i]);
131  }
132}
Note: See TracBrowser for help on using the repository browser.