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.IOException; 28 import java.nio.charset.StandardCharsets; 29 import java.nio.file.*; 30 import java.util.Set; 31 32 public class CheckableRepository { 33 private static String markerLine = "The very first line\n"; 34 35 private static Path checkableFile(Path path) throws IOException { 36 try (var checkable = Files.newBufferedReader(path.resolve(".checkable/name.txt"), StandardCharsets.UTF_8)) { 37 var checkableName = checkable.readLine(); 38 return path.resolve(checkableName); 39 } 40 } 41 42 public static Repository init(Path path, VCS vcs, Path appendableFilePath, Set<String> checks) throws IOException { 43 var repo = Repository.init(path, vcs); 44 45 Files.createDirectories(path.resolve(".checkable")); 46 try (var output = Files.newBufferedWriter(path.resolve(".checkable/name.txt"))) { 47 output.write(appendableFilePath.toString()); 48 } 49 repo.add(path.resolve(".checkable/name.txt")); 50 51 var initialFile = path.resolve(appendableFilePath); 52 try (var output = Files.newBufferedWriter(initialFile)) { 53 output.append(markerLine); 54 } 55 repo.add(initialFile); 56 57 Files.createDirectories(path.resolve(".jcheck")); 58 var checkConf = path.resolve(".jcheck/conf"); 59 try (var output = Files.newBufferedWriter(checkConf)) { 60 output.append("[general]\n"); 61 output.append("project=test\n"); 62 output.append("\n"); 63 output.append("[checks]\n"); 64 output.append("error="); 65 output.append(String.join(",", checks)); 66 output.append("\n\n"); 67 output.append("[census]\n"); 68 output.append("version=0\n"); 69 output.append("domain=openjdk.java.net\n"); 70 output.append("\n"); 71 output.append("[checks \"whitespace\"]\n"); 72 output.append("files=.*\\.txt\n"); 73 output.append("\n"); 74 output.append("[checks \"reviewers\"]\n"); 75 output.append("minimum=1\n"); 76 } 77 repo.add(checkConf); 78 79 repo.commit("Initial commit", "testauthor", "ta@none.none"); 80 81 return repo; 82 } 83 84 public static Repository init(Path path, VCS vcs, Path appendableFilePath) throws IOException { 85 return init(path, vcs, appendableFilePath, Set.of("author", "reviewers", "whitespace")); 86 } 87 88 public static Repository init(Path path, VCS vcs) throws IOException { 89 return init(path, vcs, Path.of("appendable.txt")); 90 } 91 92 public static Hash appendAndCommit(Repository repo) throws IOException { 93 return appendAndCommit(repo, "This is a new line"); 94 } 95 96 public static Hash appendAndCommit(Repository repo, String body) throws IOException { 97 return appendAndCommit(repo, body, "Append commit"); 98 } 99 100 public static Hash appendAndCommit(Repository repo, String body, String message) throws IOException { 101 return appendAndCommit(repo, body, message, "testauthor", "ta@none.none"); 102 } 103 104 public static Hash appendAndCommit(Repository repo, String body, String message, String authorName, String authorEmail) throws IOException { 105 return appendAndCommit(repo, body, message, authorName, authorEmail, authorName, authorEmail); 106 } 107 108 public static Hash appendAndCommit(Repository repo, String body, String message, String authorName, String authorEmail, 109 String committerName, String committerEmail) throws IOException { 110 var file = checkableFile(repo.root()); 111 try (var output = Files.newBufferedWriter(file, StandardOpenOption.APPEND)) { 112 output.append(body); 113 output.append("\n"); 114 } 115 repo.add(file); 116 117 return repo.commit(message, authorName, authorEmail, committerName, committerEmail); 118 } 119 120 public static Hash replaceAndCommit(Repository repo, String body) throws IOException { 121 return replaceAndCommit(repo, body, "Replace commit", "testauthor", "ta@none.none"); 122 } 123 124 public static Hash replaceAndCommit(Repository repo, String body, String message, String authorName, String authorEmail) throws IOException { 125 var file = checkableFile(repo.root()); 126 try (var output = Files.newBufferedWriter(file)) { 127 output.append(markerLine); 128 output.append(body); 129 output.append("\n"); 130 } 131 repo.add(file); 132 133 return repo.commit(message, authorName, authorEmail); 134 } 135 136 public static boolean hasBeenEdited(Repository repo) throws IOException { 137 var file = checkableFile(repo.root()); 138 var lines = Files.readAllLines(file); 139 return lines.size() > 1; 140 } 141 }