source: trunk/test/copyright_test.cc @ 687

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

Merged patch release 0.6.6 to trunk. Delta 0.6.6 - 0.6.5

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
RevLine 
[592]1// $Id: copyright_test.cc 687 2008-08-04 19:37:10Z peter $
2
3/*
4  Copyright (C) 2008 Peter Johansson
5
[687]6  This file is part of svndigest, http://dev.thep.lu.se/svndigest
[592]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
[596]24#include "Suite.h"
25
[592]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>
[597]34#include <sstream>
[592]35#include <string>
[594]36#include <vector>
[592]37
[594]38std::vector<std::string> copyright_lines(std::istream&);
39
[592]40int main( int argc, char* argv[])
41{
42  using namespace theplu::svndigest;
43  bool verbose=false;
[594]44  bool ok=true;
[592]45  if (argc>1 && argv[1]==std::string("-v") )
46    verbose=true;
47
[596]48  std::string root=test::filename("toy_project");
[594]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());
[595]60  std::vector<std::string> copyrights_old=copyright_lines(is);
[594]61  is.close();
62  is.clear(std::ios::goodbit);
63
[595]64  if (copyrights_old.size()!=1) {
[594]65    if (verbose){
[595]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";
[594]69    }
70    ok = false;
71  }
72  else if (verbose)
[638]73    std::cout << "File contains 1 copyright line.\n"; 
[594]74
[597]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
[592]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;
[594]95  File file(0,filename,"");
[592]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;
[597]104  alias["jari"]=Alias("jh", 1);
105  alias["peter"]=Alias("pj", 2);
106 
[592]107  file.print_copyright(alias, verbose);
108
[595]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
[597]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()) {
[595]118    ok = false;
119    if (verbose)
[597]120      std::cout << "ERROR: expected " << copyright_correct.size() 
[638]121                << " lines of Copyright (C)\n"
122                << "But found " << copyrights.size() << " lines.\n";
[595]123  }
[597]124  else {
[595]125    for (size_t i=0; i<copyrights.size(); ++i)
[597]126      if (copyrights[i]!=copyright_correct[i]){
127        ok=false;
128        if (verbose)
129          std::cerr << "ERROR: found '" << copyrights[i] << "'\n"
130                    << "expected: '" << copyright_correct[i] << "'\n";
131      }
132
[595]133  }
134
[597]135
[594]136  // Restoring file
137  std::ofstream os(filename.c_str());
138  assert(os.good());
139  os << original_file;
[599]140  os.close();
[594]141
142  if (ok) {
143    if (verbose)
144      std::cout << "Test is Ok!" << std::endl;
145    return 0;
146  }
[592]147  if (verbose)
[594]148    std::cout << "Test failed." << std::endl;
149  return 1;
[592]150}
151
[594]152std::vector<std::string> copyright_lines(std::istream& is)
153{
154  using namespace theplu::svndigest;
155  std::vector<std::string> res;
156  std::string line;
157  while (std::getline(is, line)){
158    if (match_begin(line.begin(), line.end(), "Copyright (C)"))
159      res.push_back(line);
160  }
161 
162  return res;
163}
164
Note: See TracBrowser for help on using the repository browser.