]> source.dussan.org Git - jgit.git/commitdiff
[test] Fix OpenSshConfigFileTest for Windows 82/190182/1
authorThomas Wolf <thomas.wolf@paranor.ch>
Sun, 30 Jan 2022 19:59:26 +0000 (20:59 +0100)
committerThomas Wolf <thomas.wolf@paranor.ch>
Sun, 30 Jan 2022 19:59:26 +0000 (20:59 +0100)
The tests assumed that a path like "/tmp" was an absolute path, and
also compared against strings with forward slashes. On Windows, "/tmp"
is not an absolute path and thus resolved against the current directory,
and the separator is a backslash.

Change the tests to use ~/ notation, and test paths resolved against
the (mocked) user home directory. That way, the tests are independent
of the file system used.

Bug: 550111
Change-Id: I1c31608ca83c8d8586256d1586a792e4a33cfaa4
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/transport/ssh/OpenSshConfigFileTest.java

index 1b420e9d86d5f4cb9ef1ecfc9814e37e9752786d..d065280778887e36ff6103ccd94719aa20e91fb7 100644 (file)
@@ -369,20 +369,21 @@ public class OpenSshConfigFileTest extends RepositoryTestCase {
 
        @Test
        public void testListValueSingle() throws Exception {
-               config("Host orcz\nUserKnownHostsFile /foo/bar\n");
+               config("Host orcz\nUserKnownHostsFile ~/foo/bar\n");
                final HostConfig c = lookup("orcz");
                assertNotNull(c);
-               assertEquals("/foo/bar", c.getValue("UserKnownHostsFile"));
+               assertEquals(new File(home, "foo/bar").getPath(),
+                               c.getValue("UserKnownHostsFile"));
        }
 
        @Test
        public void testListValueMultiple() throws Exception {
                // Tilde expansion occurs within the parser
-               config("Host orcz\nUserKnownHostsFile \"~/foo/ba z\" /foo/bar \n");
+               config("Host orcz\nUserKnownHostsFile \"~/foo/ba z\" ~/foo/bar \n");
                final HostConfig c = lookup("orcz");
                assertNotNull(c);
                assertArrayEquals(new Object[] { new File(home, "foo/ba z").getPath(),
-                               "/foo/bar" },
+                               new File(home, "foo/bar").getPath() },
                                c.getValues("UserKnownHostsFile").toArray());
        }
 
@@ -403,22 +404,23 @@ public class OpenSshConfigFileTest extends RepositoryTestCase {
 
        @Test
        public void testIdentityFile() throws Exception {
-               config("Host orcz\nIdentityFile \"~/foo/ba z\"\nIdentityFile /foo/bar");
+               config("Host orcz\nIdentityFile \"~/foo/ba z\"\nIdentityFile ~/foo/bar");
                final HostConfig h = lookup("orcz");
                assertNotNull(h);
                // Does tilde replacement
                assertArrayEquals(new Object[] { new File(home, "foo/ba z").getPath(),
-                               "/foo/bar" },
+                               new File(home, "foo/bar").getPath() },
                                h.getValues(SshConstants.IDENTITY_FILE).toArray());
        }
 
        @Test
        public void testMultiIdentityFile() throws Exception {
-               config("IdentityFile \"~/foo/ba z\"\nHost orcz\nIdentityFile /foo/bar\nHOST *\nIdentityFile /foo/baz");
+               config("IdentityFile \"~/foo/ba z\"\nHost orcz\nIdentityFile ~/foo/bar\nHOST *\nIdentityFile ~/foo/baz");
                final HostConfig h = lookup("orcz");
                assertNotNull(h);
                assertArrayEquals(new Object[] { new File(home, "foo/ba z").getPath(),
-                               "/foo/bar", "/foo/baz" },
+                               new File(home, "foo/bar").getPath(),
+                               new File(home, "foo/baz").getPath() },
                                h.getValues(SshConstants.IDENTITY_FILE).toArray());
        }
 
@@ -434,23 +436,23 @@ public class OpenSshConfigFileTest extends RepositoryTestCase {
 
        @Test
        public void testPattern() throws Exception {
-               config("Host repo.or.cz\nIdentityFile ~/foo/bar\nHOST *.or.cz\nIdentityFile /foo/baz");
+               config("Host repo.or.cz\nIdentityFile ~/foo/bar\nHOST *.or.cz\nIdentityFile ~/foo/baz");
                final HostConfig h = lookup("repo.or.cz");
                assertNotNull(h);
                assertIdentity(new File(home, "foo/bar"), h);
                assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath(),
-                               "/foo/baz" },
+                               new File(home, "foo/baz").getPath() },
                                h.getValues(SshConstants.IDENTITY_FILE).toArray());
        }
 
        @Test
        public void testMultiHost() throws Exception {
-               config("Host orcz *.or.cz\nIdentityFile ~/foo/bar\nHOST *.or.cz\nIdentityFile /foo/baz");
+               config("Host orcz *.or.cz\nIdentityFile ~/foo/bar\nHOST *.or.cz\nIdentityFile ~/foo/baz");
                final HostConfig h1 = lookup("repo.or.cz");
                assertNotNull(h1);
                assertIdentity(new File(home, "foo/bar"), h1);
                assertArrayEquals(new Object[] { new File(home, "foo/bar").getPath(),
-                               "/foo/baz" },
+                               new File(home, "foo/baz").getPath() },
                                h1.getValues(SshConstants.IDENTITY_FILE).toArray());
                final HostConfig h2 = lookup("orcz");
                assertNotNull(h2);
@@ -547,18 +549,20 @@ public class OpenSshConfigFileTest extends RepositoryTestCase {
 
        @Test
        public void testEnVarSubstitution() throws Exception {
-               config("Host orcz\nIdentityFile /tmp/${TST_VAR}\n"
-                               + "CertificateFile /tmp/${}/foo\nUser ${TST_VAR}\nIdentityAgent /tmp/${TST_VAR/bar");
+               config("Host orcz\nIdentityFile ~/tmp/${TST_VAR}\n"
+                               + "CertificateFile ~/tmp/${}/foo\nUser ${TST_VAR}\nIdentityAgent ~/tmp/${TST_VAR/bar");
                HostConfig h = lookup("orcz");
                assertNotNull(h);
-               assertEquals("/tmp/TEST",
+               File tmp = new File(home, "tmp");
+               assertEquals(new File(tmp, "TEST").getPath(),
                                h.getValue(SshConstants.IDENTITY_FILE));
                // No variable name
-               assertEquals("/tmp/${}/foo", h.getValue(SshConstants.CERTIFICATE_FILE));
+               assertEquals(new File(new File(tmp, "${}"), "foo").getPath(),
+                               h.getValue(SshConstants.CERTIFICATE_FILE));
                // User doesn't get env var substitution:
                assertUser("${TST_VAR}", h);
                // Unterminated:
-               assertEquals("/tmp/${TST_VAR/bar",
+               assertEquals(new File(new File(tmp, "${TST_VAR"), "bar").getPath(),
                                h.getValue(SshConstants.IDENTITY_AGENT));
        }
 
@@ -623,13 +627,16 @@ public class OpenSshConfigFileTest extends RepositoryTestCase {
 
        @Test
        public void testMultipleMatch() throws Exception {
-               config("Host foo.bar\nPort 29418\nIdentityFile /foo\n\n"
-                               + "Host *.bar\nPort 22\nIdentityFile /bar\n"
-                               + "Host foo.bar\nPort 47\nIdentityFile /baz\n");
+               config("Host foo.bar\nPort 29418\nIdentityFile ~/foo\n\n"
+                               + "Host *.bar\nPort 22\nIdentityFile ~/bar\n"
+                               + "Host foo.bar\nPort 47\nIdentityFile ~/baz\n");
                HostConfig h = lookup("foo.bar");
                assertNotNull(h);
                assertPort(29418, h);
-               assertArrayEquals(new Object[] { "/foo", "/bar", "/baz" },
+               assertArrayEquals(
+                               new Object[] { new File(home, "foo").getPath(),
+                                               new File(home, "bar").getPath(),
+                                               new File(home, "baz").getPath() },
                                h.getValues(SshConstants.IDENTITY_FILE).toArray());
        }