source: trunk/test/copyright_test.cc @ 744

Last change on this file since 744 was 744, checked in by Peter Johansson, 13 years ago

fixes #355 - skipping tests. Added some support in test::Suite to handle the test repo. There is a bool in constructor telling if the tests needs the test repo. test::Suite uses a new script svn_update.sh to check out and update the test repo. This script is used in test_repo.sh as well.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1// $Id: copyright_test.cc 744 2009-01-08 22:37:12Z peter $
2
3/*
4  Copyright (C) 2008, 2009 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
24#include "Configuration.h"
25#include "File.h"
26#include "SVN.h"
27#include "SVNinfo.h"
28
29#include <cassert>
30#include <iostream>
31#include <fstream>
32#include <sstream>
33#include <string>
34#include <vector>
35
36std::vector<std::string> copyright_lines(std::istream&);
37
38int main( int argc, char* argv[])
39{
40  using namespace theplu::svndigest;
41  test::Suite suite(argc, argv, true);
42
43  std::string root=test::filename("toy_project");
44  std::string filename = root + "/README";
45
46  // Saving original file
47  std::ifstream is(filename.c_str());
48  assert(is.good());
49  std::string original_file;
50  std::getline(is, original_file, '\0');
51  is.close();
52  is.clear(std::ios::goodbit);
53
54  is.open(filename.c_str());
55  std::vector<std::string> copyrights_old=copyright_lines(is);
56  is.close();
57  is.clear(std::ios::goodbit);
58
59  if (copyrights_old.size()!=1) {
60    suite.out() << copyrights_old.size() << " Copyright lines\n";
61    for (size_t i=0; i<copyrights_old.size(); ++i)
62      suite.out() << copyrights_old[i] << "\n";
63    suite.add(false);
64  }
65  suite.out() << "File contains 1 copyright line.\n"; 
66
67  // warn about missing Copyright statement only in verbose mode
68  if (suite.verbose()){
69    std::string config_str("[copyright]\nmissing-copyright-warning=yes");
70    std::stringstream ss(config_str);
71    Configuration& config = Configuration::instance();
72    config.load(ss);
73  }
74
75  suite.out() << "Create SVN instance" << std::endl;
76  SVN* svn=SVN::instance(root);
77  if (!svn)
78    return 1;
79
80  // Extract repository location
81  suite.out() << "Extract repository location" << std::endl;
82  std::string repo=SVNinfo(root).repos_root_url();
83  suite.out() << "Create File object" << std::endl;
84  File file(0,filename,"");
85 
86  suite.out() << "Get stats for file" << std::endl;
87  file.parse(suite.verbose(), true);
88 
89  suite.out() << "Updating copyright statements" << std::endl;
90  std::map<std::string, Alias> alias;
91  alias["jari"]=Alias("jh", 1);
92  alias["peter"]=Alias("pj", 2);
93 
94  file.print_copyright(alias, suite.verbose());
95
96  is.open(filename.c_str());
97  std::vector<std::string> copyrights=copyright_lines(is);
98  is.close();
99  is.clear(std::ios::goodbit);
100
101  std::vector<std::string> copyright_correct;
102  copyright_correct.push_back("Copyright (C) 2006 jh");
103  copyright_correct.push_back("Copyright (C) 2007, 2008 pj");
104  if (copyrights.size()!=copyright_correct.size()) {
105    suite.add(false);
106    suite.out() << "ERROR: expected " << copyright_correct.size() 
107                << " lines of Copyright (C)\n"
108                << "But found " << copyrights.size() << " lines.\n";
109  }
110  else {
111    for (size_t i=0; i<copyrights.size(); ++i)
112      if (copyrights[i]!=copyright_correct[i]){
113        suite.add(false);
114        suite.out() << "ERROR: found '" << copyrights[i] << "'\n"
115                    << "expected: '" << copyright_correct[i] << "'\n";
116      }
117
118  }
119
120
121  // Restoring file
122  std::ofstream os(filename.c_str());
123  assert(os.good());
124  os << original_file;
125  os.close();
126
127  if (suite.ok()) {
128    suite.out() << "Test is Ok!" << std::endl;
129    return 0;
130  }
131  suite.out() << "Test failed." << std::endl;
132  return 1;
133}
134
135std::vector<std::string> copyright_lines(std::istream& is)
136{
137  using namespace theplu::svndigest;
138  std::vector<std::string> res;
139  std::string line;
140  while (std::getline(is, line)){
141    if (match_begin(line.begin(), line.end(), "Copyright (C)"))
142      res.push_back(line);
143  }
144 
145  return res;
146}
147
Note: See TracBrowser for help on using the repository browser.