source: trunk/test/copyright.cc @ 1238

Last change on this file since 1238 was 1238, checked in by Peter Johansson, 12 years ago

remove print_copyright from Node interface since we use traverse to traverse node tree and only need to update copyright on File

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