source: trunk/test/copyright.cc @ 594

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

working on copyright test

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1// $Id: copyright.cc 594 2008-04-14 03:24:02Z 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 "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 <string>
33#include <vector>
34
35std::vector<std::string> copyright_lines(std::istream&);
36
37int main( int argc, char* argv[])
38{
39  using namespace theplu::svndigest;
40  bool verbose=false;
41  bool ok=true;
42  if (argc>1 && argv[1]==std::string("-v") )
43    verbose=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=copyright_lines(is);
58  is.close();
59  is.clear(std::ios::goodbit);
60
61  if (copyrights.size()!=1) {
62    if (verbose){
63      std::cout << copyrights.size() << " Copyright lines\n";
64      for (size_t i=0; i<copyrights.size(); ++i)
65        std::cout << copyrights[i] << "\n";
66    }
67    ok = false;
68  }
69  else if (verbose)
70    std::cout << "File containes 1 copyright line.\n"; 
71
72  if (verbose)
73    std::cout << "Create SVN instance" << std::endl;
74  SVN* svn=SVN::instance(root);
75  if (!svn)
76    return 1;
77
78  // Extract repository location
79  if (verbose)
80    std::cout << "Extract repository location" << std::endl;
81  std::string repo=SVNinfo(root).repos_root_url();
82  if (verbose)
83    std::cout << "Create File object" << std::endl;
84  File file(0,filename,"");
85 
86  if (verbose)
87    std::cout << "Get stats for file" << std::endl;
88  file.parse(verbose, true);
89 
90  if (verbose)
91    std::cout << "Updating copyright statements" << std::endl;
92  std::map<std::string, Alias> alias;
93  file.print_copyright(alias, verbose);
94
95  // Restoring file
96  std::ofstream os(filename.c_str());
97  assert(os.good());
98  os << original_file;
99
100  if (ok) {
101    if (verbose)
102      std::cout << "Test is Ok!" << std::endl;
103    return 0;
104  }
105  if (verbose)
106    std::cout << "Test failed." << std::endl;
107  return 1;
108}
109
110std::vector<std::string> copyright_lines(std::istream& is)
111{
112  using namespace theplu::svndigest;
113  std::vector<std::string> res;
114  std::string line;
115  while (std::getline(is, line)){
116    if (match_begin(line.begin(), line.end(), "Copyright (C)"))
117      res.push_back(line);
118  }
119 
120  return res;
121}
122
Note: See TracBrowser for help on using the repository browser.