source: trunk/test/copyright_test.cc @ 624

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

fixing #321

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1// $Id: copyright_test.cc 624 2008-04-23 21:57:56Z peter $
2
3/*
4  Copyright (C) 2008 Peter Johansson
5
6  This file is part of svndigest, http://trac.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 2 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 this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "Suite.h"
25
26#include "Configuration.h"
27#include "File.h"
28#include "SVN.h"
29#include "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  bool verbose=false;
44  bool ok=true;
45  if (argc>1 && argv[1]==std::string("-v") )
46    verbose=true;
47
48  std::string root=test::filename("toy_project");
49  std::string filename = root + "/README";
50
51  // Saving original file
52  std::ifstream is(filename.c_str());
53  assert(is.good());
54  std::string original_file;
55  std::getline(is, original_file, '\0');
56  is.close();
57  is.clear(std::ios::goodbit);
58
59  is.open(filename.c_str());
60  std::vector<std::string> copyrights_old=copyright_lines(is);
61  is.close();
62  is.clear(std::ios::goodbit);
63
64  if (copyrights_old.size()!=1) {
65    if (verbose){
66      std::cout << copyrights_old.size() << " Copyright lines\n";
67      for (size_t i=0; i<copyrights_old.size(); ++i)
68        std::cout << copyrights_old[i] << "\n";
69    }
70    ok = false;
71  }
72  else if (verbose)
73    std::cout << "File containes 1 copyright line.\n"; 
74
75  // warn about missing Copyright statement only in verbose mode
76  if (verbose){
77    std::string config_str("[copyright]\nmissing-copyright-warning=yes");
78    std::stringstream ss(config_str);
79    Configuration& config = Configuration::instance();
80    config.load(ss);
81  }
82
83  if (verbose)
84    std::cout << "Create SVN instance" << std::endl;
85  SVN* svn=SVN::instance(root);
86  if (!svn)
87    return 1;
88
89  // Extract repository location
90  if (verbose)
91    std::cout << "Extract repository location" << std::endl;
92  std::string repo=SVNinfo(root).repos_root_url();
93  if (verbose)
94    std::cout << "Create File object" << std::endl;
95  File file(0,filename,"");
96 
97  if (verbose)
98    std::cout << "Get stats for file" << std::endl;
99  file.parse(verbose, true);
100 
101  if (verbose)
102    std::cout << "Updating copyright statements" << std::endl;
103  std::map<std::string, Alias> alias;
104  alias["jari"]=Alias("jh", 1);
105  alias["peter"]=Alias("pj", 2);
106 
107  file.print_copyright(alias, verbose);
108
109  is.open(filename.c_str());
110  std::vector<std::string> copyrights=copyright_lines(is);
111  is.close();
112  is.clear(std::ios::goodbit);
113
114  std::vector<std::string> copyright_correct;
115  copyright_correct.push_back("Copyright (C) 2006 jh");
116  copyright_correct.push_back("Copyright (C) 2007, 2008 pj");
117  if (copyrights.size()!=copyright_correct.size()) {
118    ok = false;
119    if (verbose)
120      std::cout << "ERROR: expected " << copyright_correct.size() 
121                << "lines of Copyright (C)\n";
122  }
123  else {
124    for (size_t i=0; i<copyrights.size(); ++i)
125      if (copyrights[i]!=copyright_correct[i]){
126        ok=false;
127        if (verbose)
128          std::cerr << "ERROR: found '" << copyrights[i] << "'\n"
129                    << "expected: '" << copyright_correct[i] << "'\n";
130      }
131
132  }
133
134
135  // Restoring file
136  std::ofstream os(filename.c_str());
137  assert(os.good());
138  os << original_file;
139  os.close();
140
141  if (ok) {
142    if (verbose)
143      std::cout << "Test is Ok!" << std::endl;
144    return 0;
145  }
146  if (verbose)
147    std::cout << "Test failed." << std::endl;
148  return 1;
149}
150
151std::vector<std::string> copyright_lines(std::istream& is)
152{
153  using namespace theplu::svndigest;
154  std::vector<std::string> res;
155  std::string line;
156  while (std::getline(is, line)){
157    if (match_begin(line.begin(), line.end(), "Copyright (C)"))
158      res.push_back(line);
159  }
160 
161  return res;
162}
163
Note: See TracBrowser for help on using the repository browser.