View Javadoc
1   /*
2    * Copyright (C) 2021, 2022 SAP SE and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.lib;
12  
13  import static org.junit.Assert.assertEquals;
14  
15  import java.io.File;
16  import java.io.IOException;
17  
18  import org.eclipse.jgit.errors.ConfigInvalidException;
19  import org.eclipse.jgit.junit.JGitTestUtil;
20  import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
21  import org.junit.Rule;
22  import org.junit.Test;
23  import org.junit.rules.TemporaryFolder;
24  
25  /*
26   * This test was moved from ConfigTest to allow skipping it when running the
27   * test using bazel which doesn't allow tests to create files in the home
28   * directory
29   */
30  public class CommitTemplateConfigTest {
31  
32  	@Rule
33  	public TemporaryFolder tmp = new TemporaryFolder();
34  
35  	@Test
36  	public void testCommitTemplatePathInHomeDirecory()
37  			throws ConfigInvalidException, IOException {
38  		Config config = new Config(null);
39  		File tempFile = tmp.newFile("testCommitTemplate-");
40  		File workTree = tmp.newFolder("dummy-worktree");
41  		Repository repo = FileRepositoryBuilder.create(workTree);
42  		String templateContent = "content of the template";
43  		JGitTestUtil.write(tempFile, templateContent);
44  		// proper evaluation of the ~/ directory
45  		String homeDir = System.getProperty("user.home");
46  		File tempFileInHomeDirectory = File.createTempFile("fileInHomeFolder",
47  				".tmp", new File(homeDir));
48  		tempFileInHomeDirectory.deleteOnExit();
49  		JGitTestUtil.write(tempFileInHomeDirectory, templateContent);
50  		String expectedTemplatePath = "~/" + tempFileInHomeDirectory.getName();
51  		config = ConfigTest
52  				.parse("[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
53  		String templatePath = config.get(CommitConfig.KEY)
54  				.getCommitTemplatePath();
55  		assertEquals(expectedTemplatePath, templatePath);
56  		assertEquals(templateContent,
57  				config.get(CommitConfig.KEY).getCommitTemplateContent(repo));
58  	}
59  }