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("jbs=tstprj\n"); 62 output.append("\n"); 63 output.append("[checks]\n"); 64 output.append("error=author,reviewers,whitespace\n"); 65 output.append("\n"); 66 output.append("[census]\n"); 67 output.append("version=0\n"); 68 output.append("domain=openjdk.java.net\n"); 69 output.append("\n"); 70 output.append("[checks \"whitespace\"]\n"); 71 output.append("files=.*\\.txt\n"); 72 output.append("\n"); 73 output.append("[checks \"reviewers\"]\n"); 74 output.append("minimum=1\n"); 75 } 76 repo.add(checkConf); 77 78 repo.commit("Initial commit", "testauthor", "ta@none.none"); 79 80 return repo; 81 } 82 83 public static Repository init(Path path, VCS vcs) throws IOException { 84 return init(path, vcs, Path.of("appendable.txt")); 85 } 86 87 public static Hash appendAndCommit(Repository repo) throws IOException { 88 return appendAndCommit(repo, "This is a new line"); 89 } 90 91 public static Hash appendAndCommit(Repository repo, String body) throws IOException { 92 return appendAndCommit(repo, body, "Append commit"); 93 } 94 95 public static Hash appendAndCommit(Repository repo, String body, String message) throws IOException { 96 return appendAndCommit(repo, body, message, "testauthor", "ta@none.none"); 97 } 98 99 public static Hash appendAndCommit(Repository repo, String body, String message, String authorName, String authorEmail) throws IOException { 100 return appendAndCommit(repo, body, message, authorName, authorEmail, authorName, authorEmail); 101 } 102 103 public static Hash appendAndCommit(Repository repo, String body, String message, String authorName, String authorEmail, 104 String committerName, String committerEmail) throws IOException { 105 var file = checkableFile(repo.root()); 106 try (var output = Files.newBufferedWriter(file, StandardOpenOption.APPEND)) { 107 output.append(body); 108 output.append("\n"); 109 } 110 repo.add(file); 111 112 return repo.commit(message, authorName, authorEmail, committerName, committerEmail); 113 } 114 115 public static Hash replaceAndCommit(Repository repo, String body) throws IOException { 116 return replaceAndCommit(repo, body, "Replace commit", "testauthor", "ta@none.none"); 117 } 118 119 public static Hash replaceAndCommit(Repository repo, String body, String message, String authorName, String authorEmail) throws IOException { 120 var file = checkableFile(repo.root()); 121 try (var output = Files.newBufferedWriter(file)) { 122 output.append(markerLine); 123 output.append(body); 124 output.append("\n"); 125 } 126 repo.add(file); 127 128 return repo.commit(message, authorName, authorEmail); 129 } 130 131 public static boolean hasBeenEdited(Repository repo) throws IOException { 132 var file = checkableFile(repo.root()); 133 var lines = Files.readAllLines(file); 134 return lines.size() > 1; 135 } 136 }