mirror of
https://github.com/Murmele/Gittyup.git
synced 2024-09-11 02:35:41 +03:00
Fix local remotes using Windows paths
Add tests for SSH config URL rewriting
This commit is contained in:
parent
047e2252b4
commit
5b50913de3
@ -62,7 +62,8 @@ QString keyFile(const QString &path = QString()) {
|
||||
}
|
||||
|
||||
static QRegularExpression urlRegex{"^[0-9a-zA-Z+-]+://"};
|
||||
static QRegularExpression sshRegex{"^(([^:@/\\\\]+)@)?([0-9a-zA-Z+.-]+):(.*)$"};
|
||||
static QRegularExpression sshRegex{
|
||||
"^(([^:@/\\\\]+)@)?([0-9a-zA-Z+.-]{2,}):(.*)$"};
|
||||
QUrl parseUrl(const QString &url) {
|
||||
QUrl res;
|
||||
|
||||
|
@ -76,6 +76,7 @@ target_include_directories(testlib PRIVATE ${CMAKE_SOURCE_DIR}/src)
|
||||
target_compile_definitions(
|
||||
testlib
|
||||
PRIVATE TESTREPOSITORIES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/testRepos"
|
||||
PUBLIC SSH_CONFIG="${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
PUBLIC GIT_EXECUTABLE="${GIT_EXECUTABLE}")
|
||||
|
||||
# Add tests.
|
||||
@ -100,3 +101,4 @@ test(NAME rebase)
|
||||
test(NAME TreeView)
|
||||
test(NAME Submodule)
|
||||
test(NAME referencelist)
|
||||
test(NAME SshConfig)
|
||||
|
196
test/SshConfig.cpp
Normal file
196
test/SshConfig.cpp
Normal file
@ -0,0 +1,196 @@
|
||||
//
|
||||
// Copyright (c) 2022, Gittyup Team
|
||||
//
|
||||
// This software is licensed under the MIT License. The LICENSE.md file
|
||||
// describes the conditions under which this software may be distributed.
|
||||
//
|
||||
// Author: Kas
|
||||
//
|
||||
|
||||
#include "Test.h"
|
||||
#include "git/Remote.h"
|
||||
#include "git2/buffer.h"
|
||||
#include "qtestcase.h"
|
||||
|
||||
class Callbacks : public git::Remote::Callbacks {
|
||||
private:
|
||||
QString config;
|
||||
|
||||
public:
|
||||
Callbacks(const QString &url, const QString &config)
|
||||
: git::Remote::Callbacks(url), config(config) {}
|
||||
|
||||
virtual QString configFilePath() const override {
|
||||
return QDir{SSH_CONFIG}.filePath(config);
|
||||
}
|
||||
};
|
||||
|
||||
class TestSshConfig : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void resolveSsh();
|
||||
void resolveSshUrl();
|
||||
void resolveGitSshUrl();
|
||||
void resolveGitUrl();
|
||||
void resolveHttpUrl();
|
||||
void resolvePath();
|
||||
|
||||
private:
|
||||
static QString transformUrl(const QString &url, const QString config) {
|
||||
auto callbacks = Callbacks(url, config);
|
||||
git_buf buf;
|
||||
buf.asize = 0;
|
||||
buf.size = 0;
|
||||
buf.ptr = nullptr;
|
||||
|
||||
Callbacks::url(&buf, url.toUtf8().data(), 0, &callbacks);
|
||||
|
||||
QString res = QString::fromUtf8(buf.ptr, (int)buf.size);
|
||||
git_buf_dispose(&buf);
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
void TestSshConfig::resolveSsh() {
|
||||
QCOMPARE(transformUrl("testhost:repo", "ssh_config"),
|
||||
QString("ssh://anotheruser@resolvedhost:12345/repo"));
|
||||
QCOMPARE(transformUrl("specified@testhost:/repo", "ssh_config"),
|
||||
"ssh://specified@resolvedhost:12345/repo");
|
||||
QCOMPARE(transformUrl("diffport:", "ssh_config"),
|
||||
"ssh://defaultuser@diffport:4321/");
|
||||
QCOMPARE(transformUrl("diffuser:", "ssh_config"),
|
||||
"ssh://seconduser@diffuser:2222/");
|
||||
QCOMPARE(transformUrl("diffhost:", "ssh_config"),
|
||||
"ssh://defaultuser@secondhost:2222/");
|
||||
QCOMPARE(transformUrl("unconfigured:", "ssh_config"),
|
||||
"ssh://defaultuser@unconfigured:2222/");
|
||||
QCOMPARE(transformUrl("unconfigured:/", "ssh_config"),
|
||||
"ssh://defaultuser@unconfigured:2222/");
|
||||
QCOMPARE(transformUrl("unconfigured:/repo", "ssh_config"),
|
||||
"ssh://defaultuser@unconfigured:2222/repo");
|
||||
QCOMPARE(transformUrl("unconfigured:/repo/", "ssh_config"),
|
||||
"ssh://defaultuser@unconfigured:2222/repo/");
|
||||
QCOMPARE(transformUrl("unconfigured:repo/", "ssh_config"),
|
||||
"ssh://defaultuser@unconfigured:2222/repo/");
|
||||
|
||||
QCOMPARE(transformUrl("testhost:repo", "ssh_config2"),
|
||||
QString("ssh://anotheruser@resolvedhost:12345/repo"));
|
||||
QCOMPARE(transformUrl("testhost:/repo", "ssh_config2"),
|
||||
"ssh://anotheruser@resolvedhost:12345/repo");
|
||||
QCOMPARE(transformUrl("specified@testhost:/repo", "ssh_config2"),
|
||||
"ssh://specified@resolvedhost:12345/repo");
|
||||
QCOMPARE(transformUrl("diffport:", "ssh_config2"), "ssh://diffport:4321/");
|
||||
QCOMPARE(transformUrl("diffuser:", "ssh_config2"),
|
||||
"ssh://seconduser@diffuser/");
|
||||
QCOMPARE(transformUrl("diffhost:", "ssh_config2"), "ssh://secondhost/");
|
||||
QCOMPARE(transformUrl("unconfigured:", "ssh_config2"), "ssh://unconfigured/");
|
||||
QCOMPARE(transformUrl("unconfigured:/", "ssh_config2"),
|
||||
"ssh://unconfigured/");
|
||||
QCOMPARE(transformUrl("unconfigured:/repo", "ssh_config2"),
|
||||
"ssh://unconfigured/repo");
|
||||
QCOMPARE(transformUrl("unconfigured:/repo/", "ssh_config2"),
|
||||
"ssh://unconfigured/repo/");
|
||||
QCOMPARE(transformUrl("unconfigured:repo/", "ssh_config2"),
|
||||
"ssh://unconfigured/repo/");
|
||||
}
|
||||
|
||||
void TestSshConfig::resolveSshUrl() {
|
||||
QCOMPARE(transformUrl("ssh://testhost/repo", "ssh_config"),
|
||||
QString("ssh://anotheruser@resolvedhost:12345/repo"));
|
||||
QCOMPARE(transformUrl("ssh://specified@testhost/repo", "ssh_config"),
|
||||
"ssh://specified@resolvedhost:12345/repo");
|
||||
QCOMPARE(transformUrl("ssh://diffport", "ssh_config"),
|
||||
"ssh://defaultuser@diffport:4321");
|
||||
QCOMPARE(transformUrl("ssh://diffuser", "ssh_config"),
|
||||
"ssh://seconduser@diffuser:2222");
|
||||
QCOMPARE(transformUrl("ssh://diffhost", "ssh_config"),
|
||||
"ssh://defaultuser@secondhost:2222");
|
||||
QCOMPARE(transformUrl("ssh://unconfigured", "ssh_config"),
|
||||
"ssh://defaultuser@unconfigured:2222");
|
||||
QCOMPARE(transformUrl("ssh://unconfigured/", "ssh_config"),
|
||||
"ssh://defaultuser@unconfigured:2222/");
|
||||
QCOMPARE(transformUrl("ssh://unconfigured/repo", "ssh_config"),
|
||||
"ssh://defaultuser@unconfigured:2222/repo");
|
||||
QCOMPARE(transformUrl("ssh://unconfigured/repo/", "ssh_config"),
|
||||
"ssh://defaultuser@unconfigured:2222/repo/");
|
||||
|
||||
QCOMPARE(transformUrl("ssh://testhost/repo", "ssh_config2"),
|
||||
QString("ssh://anotheruser@resolvedhost:12345/repo"));
|
||||
QCOMPARE(transformUrl("ssh://specified@testhost/repo", "ssh_config2"),
|
||||
"ssh://specified@resolvedhost:12345/repo");
|
||||
QCOMPARE(transformUrl("ssh://diffport", "ssh_config2"),
|
||||
"ssh://diffport:4321");
|
||||
QCOMPARE(transformUrl("ssh://diffuser", "ssh_config2"),
|
||||
"ssh://seconduser@diffuser");
|
||||
QCOMPARE(transformUrl("ssh://diffhost", "ssh_config2"), "ssh://secondhost");
|
||||
QCOMPARE(transformUrl("ssh://unconfigured", "ssh_config2"),
|
||||
"ssh://unconfigured");
|
||||
QCOMPARE(transformUrl("ssh://unconfigured/", "ssh_config2"),
|
||||
"ssh://unconfigured/");
|
||||
QCOMPARE(transformUrl("ssh://unconfigured/repo", "ssh_config2"),
|
||||
"ssh://unconfigured/repo");
|
||||
QCOMPARE(transformUrl("ssh://unconfigured/repo/", "ssh_config2"),
|
||||
"ssh://unconfigured/repo/");
|
||||
}
|
||||
|
||||
void TestSshConfig::resolveGitSshUrl() {
|
||||
QCOMPARE(transformUrl("git+ssh://testhost/repo", "ssh_config"),
|
||||
QString("git+ssh://testhost/repo"));
|
||||
QCOMPARE(transformUrl("git+ssh://unconfigured/repo", "ssh_config"),
|
||||
QString("git+ssh://unconfigured/repo"));
|
||||
|
||||
QCOMPARE(transformUrl("git+ssh://testhost/repo", "ssh_config2"),
|
||||
QString("git+ssh://testhost/repo"));
|
||||
QCOMPARE(transformUrl("git+ssh://unconfigured/repo", "ssh_config2"),
|
||||
QString("git+ssh://unconfigured/repo"));
|
||||
}
|
||||
|
||||
void TestSshConfig::resolveGitUrl() {
|
||||
QCOMPARE(transformUrl("git://testhost/repo", "ssh_config"),
|
||||
QString("git://testhost/repo"));
|
||||
QCOMPARE(transformUrl("git://unconfigured/repo", "ssh_config"),
|
||||
QString("git://unconfigured/repo"));
|
||||
|
||||
QCOMPARE(transformUrl("git://testhost/repo", "ssh_config2"),
|
||||
QString("git://testhost/repo"));
|
||||
QCOMPARE(transformUrl("git://unconfigured/repo", "ssh_config2"),
|
||||
QString("git://unconfigured/repo"));
|
||||
}
|
||||
|
||||
void TestSshConfig::resolveHttpUrl() {
|
||||
QCOMPARE(transformUrl("http://testhost/repo", "ssh_config"),
|
||||
QString("http://testhost/repo"));
|
||||
QCOMPARE(transformUrl("http://unconfigured/repo", "ssh_config"),
|
||||
QString("http://unconfigured/repo"));
|
||||
|
||||
QCOMPARE(transformUrl("http://testhost/repo", "ssh_config2"),
|
||||
QString("http://testhost/repo"));
|
||||
QCOMPARE(transformUrl("http://unconfigured/repo", "ssh_config2"),
|
||||
QString("http://unconfigured/repo"));
|
||||
}
|
||||
|
||||
void TestSshConfig::resolvePath() {
|
||||
QCOMPARE(transformUrl("/some/path", "ssh_config"),
|
||||
QString("file:///some/path"));
|
||||
QCOMPARE(transformUrl("some/path", "ssh_config"), QString("file:some/path"));
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
QCOMPARE(transformUrl("A:/some/path", "ssh_config"),
|
||||
QString("file:///A:/some/path"));
|
||||
QCOMPARE(transformUrl("A:\\some\\path", "ssh_config"),
|
||||
QString("file:///A:/some/path"));
|
||||
QCOMPARE(transformUrl("some\\path", "ssh_config"), QString("file:some/path"));
|
||||
#else
|
||||
QCOMPARE(transformUrl("A:/some/path", "ssh_config"),
|
||||
QString("file:///A:/some/path"));
|
||||
QCOMPARE(transformUrl("A:\\some\\path", "ssh_config"),
|
||||
QString("file:///A:%5Csome%5Cpath"));
|
||||
QCOMPARE(transformUrl("some\\path", "ssh_config"),
|
||||
QString("file:some%5Cpath"));
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_MAIN(TestSshConfig)
|
||||
|
||||
#include "SshConfig.moc"
|
16
test/ssh_config
Normal file
16
test/ssh_config
Normal file
@ -0,0 +1,16 @@
|
||||
Port 2222
|
||||
User defaultuser
|
||||
|
||||
Host testhost
|
||||
Hostname resolvedhost
|
||||
Port 12345
|
||||
User anotheruser
|
||||
|
||||
Host diffport
|
||||
Port 4321
|
||||
|
||||
Host diffuser
|
||||
User seconduser
|
||||
|
||||
Host diffhost
|
||||
Hostname secondhost
|
13
test/ssh_config2
Normal file
13
test/ssh_config2
Normal file
@ -0,0 +1,13 @@
|
||||
Host testhost
|
||||
Hostname resolvedhost
|
||||
Port 12345
|
||||
User anotheruser
|
||||
|
||||
Host diffport
|
||||
Port 4321
|
||||
|
||||
Host diffuser
|
||||
User seconduser
|
||||
|
||||
Host diffhost
|
||||
Hostname secondhost
|
Loading…
Reference in New Issue
Block a user