1 /* 2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.openjdk.skara.test; 24 25 import org.openjdk.skara.vcs.*; 26 27 import java.io.*; 28 import java.nio.charset.StandardCharsets; 29 import java.nio.file.*; 30 31 public class CheckableRepository { 32 private static String markerLine = "The very first line\n"; 33 34 private static Path checkableFile(Path path) throws IOException { 35 try (var checkable = Files.newBufferedReader(path.resolve(".checkable/name.txt"), StandardCharsets.UTF_8)) { 36 var checkableName = checkable.readLine(); 37 return path.resolve(checkableName); 38 } 39 } 40 41 public static Repository init(Path path, VCS vcs, Path appendableFilePath) throws IOException { 42 var repo = Repository.init(path, vcs); 43 44 Files.createDirectories(path.resolve(".checkable")); 45 try (var output = Files.newBufferedWriter(path.resolve(".checkable/name.txt"))) { 46 output.write(appendableFilePath.toString()); 47 } 48 repo.add(path.resolve(".checkable/name.txt")); 49 50 var initialFile = path.resolve(appendableFilePath); 51 try (var output = Files.newBufferedWriter(initialFile)) { 52 output.append(markerLine); 53 } 54 repo.add(initialFile); 55 56 Files.createDirectories(path.resolve(".jcheck")); 57 var checkConf = path.resolve(".jcheck/conf"); 58 try (var output = Files.newBufferedWriter(checkConf)) { 59 output.append("[general]\n"); 60 output.append("project=test\n"); 61 output.append("\n"); 62 output.append("[checks]\n"); 63 output.append("error=author,reviewers,whitespace\n"); 64 output.append("\n"); 65 output.append("[census]\n"); 66 output.append("version=0\n"); 67 output.append("domain=openjdk.java.net\n"); 68 output.append("\n"); 69 output.append("[checks \"whitespace\"]\n"); 70 output.append("files=.*\\.txt\n"); 71 output.append("\n"); 72 output.append("[checks \"reviewers\"]\n"); 73 output.append("minimum=1\n"); 74 } 75 repo.add(checkConf); 76 77 repo.commit("Initial commit", "testauthor", "ta@none.none"); 78 79 return repo; 80 } 81 82 public static Repository init(Path path, VCS vcs) throws IOException { 83 return init(path, vcs, Path.of("appendable.txt")); 84 } 85 86 public static Hash appendAndCommit(Repository repo) throws IOException { 87 return appendAndCommit(repo, "This is a new line"); 88 } 89 90 public static Hash appendAndCommit(Repository repo, String body) throws IOException { 91 return appendAndCommit(repo, body, "Append commit"); 92 } 93 94 public static Hash appendAndCommit(Repository repo, String body, String message) throws IOException { 95 return appendAndCommit(repo, body, message, "testauthor", "ta@none.none"); 96 } 97 98 public static Hash appendAndCommit(Repository repo, String body, String message, String authorName, String authorEmail) throws IOException { 99 return appendAndCommit(repo, body, message, authorName, authorEmail, authorName, authorEmail); 100 } 101 102 public static Hash appendAndCommit(Repository repo, String body, String message, String authorName, String authorEmail, 103 String committerName, String committerEmail) throws IOException { 104 var file = checkableFile(repo.root()); 105 try (var output = Files.newBufferedWriter(file, StandardOpenOption.APPEND)) { 106 output.append(body); 107 output.append("\n"); 108 } 109 repo.add(file); 110 111 return repo.commit(message, authorName, authorEmail, committerName, committerEmail); 112 } 113 114 public static Hash replaceAndCommit(Repository repo, String body) throws IOException { 115 return replaceAndCommit(repo, body, "Replace commit", "testauthor", "ta@none.none"); 116 } 117 118 public static Hash replaceAndCommit(Repository repo, String body, String message, String authorName, String authorEmail) throws IOException { 119 var file = checkableFile(repo.root()); 120 try (var output = Files.newBufferedWriter(file)) { 121 output.append(markerLine); 122 output.append(body); 123 output.append("\n"); 124 } 125 repo.add(file); 126 127 return repo.commit(message, authorName, authorEmail); 128 } 129 130 public static boolean hasBeenEdited(Repository repo) throws IOException { 131 var file = checkableFile(repo.root()); 132 var lines = Files.readAllLines(file); 133 return lines.size() > 1; 134 } 135 }