source: trunk/test/copyright.cc @ 1321

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

updating includes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1// $Id: copyright.cc 1321 2010-11-28 23:41:31Z peter $
2
3/*
4  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2009, 2010 Peter Johansson
6
7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 3 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Suite.h"
24
25#include "lib/Configuration.h"
26#include "lib/File.h"
27#include "lib/main_utility.h"
28#include "lib/SVN.h"
29#include "lib/SVNinfo.h"
30#include "lib/utility.h"
31
32#include <cassert>
33#include <iostream>
34#include <fstream>
35#include <sstream>
36#include <string>
37#include <vector>
38
39std::vector<std::string> copyright_lines(std::istream&);
40
41int main( int argc, char* argv[])
42{
43  using namespace theplu::svndigest;
44  test::Suite suite(argc, argv, true);
45
46  std::string root="toy_project";
47  std::string filename = root + "/README";
48
49  // Saving original file
50  std::ifstream is(filename.c_str());
51  assert(is.good());
52  std::string original_file;
53  std::getline(is, original_file, '\0');
54  is.close();
55  is.clear(std::ios::goodbit);
56
57  is.open(filename.c_str());
58  std::vector<std::string> copyrights_old=copyright_lines(is);
59  is.close();
60  is.clear(std::ios::goodbit);
61
62  if (copyrights_old.size()!=1) {
63    suite.out() << copyrights_old.size() << " Copyright lines\n";
64    for (size_t i=0; i<copyrights_old.size(); ++i)
65      suite.out() << copyrights_old[i] << "\n";
66    suite.add(false);
67  }
68  suite.out() << "File contains 1 copyright line.\n"; 
69
70  // warn about missing Copyright statement only in verbose mode
71  if (suite.verbose()){
72    std::stringstream ss;
73    ss << "[copyright]\n"
74       << "missing-copyright-warning=yes\n"
75       << "[copyright-alias]\n"
76       << "jari = jh\n"
77       << "peter = pj\n";
78    Configuration& config = Configuration::instance();
79    config.load(ss);
80  }
81
82  suite.out() << "Create SVN instance" << std::endl;
83  SVN* svn=SVN::instance(root);
84  if (!svn)
85    return 1;
86
87  // Extract repository location
88  suite.out() << "Extract repository location" << std::endl;
89  std::string repo=SVNinfo(root).repos_root_url();
90  suite.out() << "Create File object" << std::endl;
91  File file(0,filename,"");
92 
93  update_copyright(file, suite.verbose(), false);
94
95  is.open(filename.c_str());
96  std::vector<std::string> copyrights=copyright_lines(is);
97  is.close();
98  is.clear(std::ios::goodbit);
99
100  std::vector<std::string> copyright_correct;
101  copyright_correct.push_back("Copyright (C) 2006 jh");
102  copyright_correct.push_back("Copyright (C) 2007, 2008 pj");
103  if (copyrights.size()!=copyright_correct.size()) {
104    suite.add(false);
105    suite.out() << "ERROR: expected " << copyright_correct.size() 
106                << " lines of Copyright (C)\n"
107                << "But found " << copyrights.size() << " lines.\n";
108  }
109  else {
110    for (size_t i=0; i<copyrights.size(); ++i)
111      if (copyrights[i]!=copyright_correct[i]){
112        suite.add(false);
113        suite.out() << "ERROR: found '" << copyrights[i] << "'\n"
114                    << "expected: '" << copyright_correct[i] << "'\n";
115      }
116
117  }
118
119
120  // Restoring file
121  std::ofstream os(filename.c_str());
122  assert(os.good());
123  os << original_file;
124  os.close();
125
126  return suite.exit_status();
127}
128
129std::vector<std::string> copyright_lines(std::istream& is)
130{
131  using namespace theplu::svndigest;
132  std::vector<std::string> res;
133  std::string line;
134  while (std::getline(is, line)){
135    if (match_begin(line.begin(), line.end(), "Copyright (C)"))
136      res.push_back(line);
137  }
138 
139  return res;
140}
141
Note: See TracBrowser for help on using the repository browser.