• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IJHack / QtPass / 23820234115

31 Mar 2026 09:27PM UTC coverage: 19.833% (+0.02%) from 19.817%
23820234115

Pull #871

github

web-flow
Merge cc087344b into 6d873a51d
Pull Request #871: CodeRabbit Generated Unit Tests: Add unit tests for PR changes

1000 of 5042 relevant lines covered (19.83%)

7.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.83
/src/util.cpp
1
// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
#include "util.h"
4
#include <QDir>
5
#include <QFileInfo>
6
#ifdef Q_OS_WIN
7
#include <windows.h>
8
#else
9
#include <sys/time.h>
10
#endif
11
#include "qtpasssettings.h"
12

13
#ifdef QT_DEBUG
14
#include "debughelper.h"
15
#endif
16

17
QProcessEnvironment Util::_env;
18
bool Util::_envInitialised = false;
19

20
void Util::initialiseEnvironment() {
14✔
21
  if (!_envInitialised) {
14✔
22
    _env = QProcessEnvironment::systemEnvironment();
1✔
23
#ifdef __APPLE__
24
    QString path = _env.value("PATH");
25
    if (!path.contains("/usr/local/MacGPG2/bin") &&
26
        QFile("/usr/local/MacGPG2/bin").exists())
27
      path += ":/usr/local/MacGPG2/bin";
28
    if (!path.contains("/usr/local/bin"))
29
      path += ":/usr/local/bin";
30
    _env.insert("PATH", path);
31
#endif
32
#ifdef Q_OS_WIN
33
    QString path = _env.value("PATH");
34
    if (!path.contains("C:\\Program Files\\WinGPG\\x86") &&
35
        QFile("C:\\Program Files\\WinGPG\\x86").exists())
36
      path += ";C:\\Program Files\\WinGPG\\x86";
37
    if (!path.contains("C:\\Program Files\\GnuPG\\bin") &&
38
        QFile("C:\\Program Files\\GnuPG\\bin").exists())
39
      path += ";C:\\Program Files\\GnuPG\\bin";
40
    _env.insert("PATH", path);
41
#endif
42
#ifdef QT_DEBUG
43
    dbg() << _env.value("PATH");
44
#endif
45
    _envInitialised = true;
1✔
46
  }
47
}
14✔
48

49
auto Util::findPasswordStore() -> QString {
2✔
50
  QString path;
2✔
51
  initialiseEnvironment();
2✔
52
  if (_env.contains("PASSWORD_STORE_DIR")) {
4✔
53
    path = _env.value("PASSWORD_STORE_DIR");
×
54
  } else {
55
#ifdef Q_OS_WIN
56
    path = QDir::homePath() + QDir::separator() + "password-store" +
57
           QDir::separator();
58
#else
59
    path = QDir::homePath() + QDir::separator() + ".password-store" +
4✔
60
           QDir::separator();
61
#endif
62
  }
63
  return Util::normalizeFolderPath(path);
4✔
64
}
65

66
auto Util::normalizeFolderPath(QString path) -> QString {
11✔
67
  if (!path.endsWith("/") && !path.endsWith(QDir::separator())) {
22✔
68
    path += QDir::separator();
6✔
69
  }
70
  return QDir::toNativeSeparators(path);
11✔
71
}
72

73
auto Util::findBinaryInPath(QString binary) -> QString {
12✔
74
  initialiseEnvironment();
12✔
75

76
  QString ret;
12✔
77

78
  binary.prepend(QDir::separator());
12✔
79

80
  if (_env.contains("PATH")) {
24✔
81
    QString path = _env.value("PATH");
24✔
82

83
    QStringList entries;
12✔
84
#ifndef Q_OS_WIN
85
    entries = path.split(':');
24✔
86
    if (entries.length() < 2) {
12✔
87
#endif
88
      entries = path.split(';');
×
89
#ifndef Q_OS_WIN
90
    }
91
#endif
92

93
    for (const QString &entryConst : entries) {
192✔
94
      QString fullPath = entryConst + binary;
178✔
95
      QScopedPointer<QFileInfo> qfi(new QFileInfo(fullPath));
178✔
96
#ifdef Q_OS_WIN
97
      if (!qfi->exists()) {
98
        QString fullPathExe = fullPath + ".exe";
99
        qfi.reset(new QFileInfo(fullPathExe));
100
      }
101

102
#endif
103
      if (!qfi->isExecutable()) {
178✔
104
        continue;
105
      }
106

107
      ret = qfi->absoluteFilePath();
20✔
108
      break;
109
    }
110
  }
111
#ifdef Q_OS_WIN
112
  if (ret.isEmpty()) {
113
    binary.remove(0, 1);
114
    binary.prepend("wsl ");
115
    QString out, err;
116
    if (Executor::executeBlocking(binary, {"--version"}, &out, &err) == 0 &&
117
        !out.isEmpty() && err.isEmpty())
118
      ret = binary;
119
  }
120
#endif
121

122
  return ret;
12✔
123
}
124

125
auto Util::checkConfig() -> bool {
1✔
126
  return !QFile(QDir(QtPassSettings::getPassStore()).filePath(".gpg-id"))
2✔
127
              .exists() ||
1✔
128
         (QtPassSettings::isUsePass()
1✔
129
              ? !QtPassSettings::getPassExecutable().startsWith("wsl ") &&
1✔
130
                    !QFile(QtPassSettings::getPassExecutable()).exists()
1✔
131
              : !QtPassSettings::getGpgExecutable().startsWith("wsl ") &&
1✔
132
                    !QFile(QtPassSettings::getGpgExecutable()).exists());
2✔
133
}
134

135
auto Util::getDir(const QModelIndex &index, bool forPass,
3✔
136
                  const QFileSystemModel &model, const StoreModel &storeModel)
137
    -> QString {
138
  QString abspath =
139
      QDir(QtPassSettings::getPassStore()).absolutePath() + QDir::separator();
9✔
140
  if (!index.isValid()) {
141
    return forPass ? "" : abspath;
2✔
142
  }
143
  QFileInfo info = model.fileInfo(storeModel.mapToSource(index));
1✔
144
  QString filePath =
145
      (info.isFile() ? info.absolutePath() : info.absoluteFilePath());
1✔
146
  if (forPass) {
1✔
147
    filePath = QDir(abspath).relativeFilePath(filePath);
×
148
  }
149
  filePath += QDir::separator();
1✔
150
  return filePath;
151
}
1✔
152

153
auto Util::endsWithGpg() -> const QRegularExpression & {
22✔
154
  static const QRegularExpression expr{"\\.gpg$"};
22✔
155
  return expr;
22✔
156
}
157

158
auto Util::protocolRegex() -> const QRegularExpression & {
3✔
159
  static const QRegularExpression regex{
160
      "((?:https?|ftp|ssh|sftp|ftps|webdav|webdavs)://[^\" <>\\)\\]\\[]+)"};
3✔
161
  return regex;
3✔
162
}
163

164
auto Util::newLinesRegex() -> const QRegularExpression & {
2✔
165
  static const QRegularExpression regex{"[\r\n]"};
2✔
166
  return regex;
2✔
167
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc