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

Last change on this file since 1246 was 1246, checked in by Peter Johansson, 11 years ago

fixes #477

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1// $Id: parameter_test.cc 1246 2010-10-30 02:41:51Z 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 <cerrno>
31#include <string>
32#include <vector>
33
34using namespace theplu::svndigest;
35
36void test_root(test::Suite& suite);
37void test_root(test::Suite& suite, const std::string& arg, 
38               const std::string& root, const std::string& root_basename);
39void parse(Parameter&, const std::vector<std::string>& argv);
40
41int main(int argc, char* argv[])
42{
43  test::Suite suite(argc, argv);
44  test_root(suite);
45  if (suite.ok())
46    return 0;
47  return 1;
48}
49
50
51void test_root(test::Suite& suite)
52{
53  suite.out() << "test --root ROOT\n";
54  test_root(suite, ".", test::abs_builddir()+"/testSubDir/parameter_test",
55            "parameter_test");
56  test_root(suite, "..", test::abs_builddir()+"/testSubDir","testSubDir");
57  test_root(suite, "../..", test::abs_builddir(), "test");
58  // test with absolute path
59  test_root(suite, test::abs_srcdir(), test::abs_srcdir(), "test");
60  // test with symbolic link
61  if (!node_exist("symlink")) {
62    if (symlink(".", "symlink")) {
63      suite.add(false);
64      suite.out() << "creating symlink failed: " << strerror(errno) << "\n";
65    }
66  }
67  test_root(suite, "symlink", 
68            test::abs_builddir()+"/testSubDir/parameter_test", "symlink");
69  test_root(suite, "symlink/.", 
70            test::abs_builddir()+"/testSubDir/parameter_test","parameter_test");
71  test_root(suite, "symlink/..", 
72            test::abs_builddir()+"/testSubDir", "testSubDir");
73  test_root(suite, test::abs_builddir()+"/testSubDir/parameter_test/symlink/..", 
74            test::abs_builddir()+"/testSubDir", "testSubDir");
75  test_root(suite, test::abs_builddir()+"/testSubDir/parameter_test/symlink", 
76            test::abs_builddir()+"/testSubDir/parameter_test", "symlink");
77 
78  try {
79    test_root(suite, "../../Makefile", test::abs_builddir()+"/Makefile", 
80              "Makefile");
81    suite.add(false);
82    suite.out() << "error: no exception thrown\n --root ../../Makefile\n";
83  }
84  catch (theplu::yat::utility::cmd_error& e) {
85    suite.out() << "caught expected errno_error: what(): " << e.what();
86  }
87}
88
89void test_root(test::Suite& suite, const std::string& arg, 
90               const std::string& root, const std::string& root_basename)
91{
92  svndigestParameter option;
93  std::vector<std::string> argv;
94  argv.push_back("svndigest");
95  argv.push_back("--no-report");
96  argv.push_back("--root");
97  argv.push_back(arg);
98  parse(option, argv);
99  if (option.root() != root) {
100    suite.add(false);
101    for (size_t i=0; i<argv.size(); ++i)
102      suite.out() << argv[i] << " ";
103    suite.out() << "\n";
104    suite.out() << "root: `" << option.root() << "'\n";
105    suite.out() << "expected: `" << root << "'\n";
106  }
107  if (option.root_basename() != root_basename) {
108    suite.add(false);
109    for (size_t i=0; i<argv.size(); ++i)
110      suite.out() << argv[i] << " ";
111    suite.out() << "\n";
112    suite.out() << "root: `" << option.root_basename() << "'\n";
113    suite.out() << "expected: `" << root_basename << "'\n";
114  }
115}
116
117
118void parse(Parameter& option, const std::vector<std::string>& vec)
119{
120  int argc = vec.size();
121  char** argv = new char*[vec.size()];
122  for (int i = 0; i<argc; ++i) {
123    argv[i] = strdup(vec[i].c_str());
124    assert(argv[i]);
125  }
126  option.parse(argc, argv);
127  for (int i = 0; i<argc; ++i) {
128    free(argv[i]);
129  }
130}
Note: See TracBrowser for help on using the repository browser.