mirror of
https://github.com/QuasarApp/CopyrightFixer.git
synced 2025-04-27 18:24:42 +00:00
ref #60 Fixing method mergeSign, unittests.
This commit is contained in:
parent
ec411c47eb
commit
ff036c87e2
@ -5,6 +5,7 @@
|
||||
//# of this license document, but changing it is not allowed.
|
||||
//#
|
||||
|
||||
#include <time.h>
|
||||
#include "signer.h"
|
||||
#include "CopyrighFixer/ifilemanager.h"
|
||||
#include <QDir>
|
||||
@ -15,11 +16,10 @@ Signer::Signer() {
|
||||
|
||||
}
|
||||
|
||||
bool Signer::checkSign(const Config &objConf) {
|
||||
|
||||
Config currConfig = objConf;
|
||||
bool Signer::processSign(const QString &pathToFile, const Config &objConf) const {
|
||||
|
||||
QDir currentFolder(currConfig.getSrcDir());
|
||||
QDir currentFolder(pathToFile);
|
||||
currentFolder.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
|
||||
|
||||
QFileInfoList folderItems(currentFolder.entryInfoList());
|
||||
@ -27,35 +27,76 @@ bool Signer::checkSign(const Config &objConf) {
|
||||
for (auto i_file: folderItems) {
|
||||
|
||||
if (i_file.isDir()) {
|
||||
processSign(i_file.filePath(), objConf);
|
||||
}
|
||||
|
||||
currConfig.setSourceDir(i_file.filePath());
|
||||
checkSign(currConfig);
|
||||
IFileManager *currFM = searchFileByExt(i_file.suffix());
|
||||
Signature signFromFile;
|
||||
Signature resultSign;
|
||||
|
||||
} else {
|
||||
if (currFM == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
IFileManager *currFM = searchFileByExt(i_file.suffix());
|
||||
Signature signFromFile;
|
||||
Signature resultSign;
|
||||
if (!currFM->read(i_file.filePath(), signFromFile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (currFM == nullptr) {
|
||||
return false;
|
||||
}
|
||||
resultSign = mergeSign(objConf.getSignVal(), signFromFile);
|
||||
|
||||
if (!currFM->read(i_file.filePath(), signFromFile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
resultSign = mergeSign(currConfig.getSignVal(), signFromFile);
|
||||
|
||||
if (!currFM->write(i_file.filePath(), resultSign)) {
|
||||
return false;
|
||||
}
|
||||
if (!currFM->write(i_file.filePath(), resultSign)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Signer::checkSign(const Config &objConf) {
|
||||
|
||||
if (!processSign(objConf.getSrcDir(), objConf)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const Signature Signer::upgradeOwner(const Signature &signConf, const Signature &fileSign) const {
|
||||
|
||||
int unixTime = time(0);
|
||||
|
||||
Signature signForSing = fileSign;
|
||||
QMap<int, CopyrighFixer::Owner> mapOwnersFile = signForSing.getMapOwn();
|
||||
mapOwnersFile.remove(fileSign.getMapOwn().cbegin().key());
|
||||
|
||||
CopyrighFixer::Owner newOwners;
|
||||
newOwners.setName(signConf.getMapOwn().cbegin().value().getOwnerName());
|
||||
newOwners.setTimePoint(unixTime);
|
||||
|
||||
mapOwnersFile.insert(unixTime, newOwners);
|
||||
signForSing.setMapOwners(mapOwnersFile);
|
||||
|
||||
return signForSing;
|
||||
|
||||
}
|
||||
|
||||
const Signature Signer::appendOwner(const Signature &signConf, const Signature &fileSign) const {
|
||||
|
||||
int unixTime = time(0);
|
||||
|
||||
Signature signForSing = fileSign;
|
||||
QMap<int, CopyrighFixer::Owner> mapOwnersFile = signForSing.getMapOwn();
|
||||
|
||||
CopyrighFixer::Owner newOwners;
|
||||
newOwners.setName(signConf.getMapOwn().cbegin().value().getOwnerName());
|
||||
newOwners.setTimePoint(unixTime);
|
||||
|
||||
mapOwnersFile.insert(unixTime, newOwners);
|
||||
signForSing.setMapOwners(mapOwnersFile);
|
||||
|
||||
return signForSing;
|
||||
}
|
||||
|
||||
const Signature Signer::mergeSign(const Signature &userSign, const Signature &fileSign) const {
|
||||
|
||||
if (!fileSign.isValid()) {
|
||||
@ -71,45 +112,28 @@ const Signature Signer::mergeSign(const Signature &userSign, const Signature &fi
|
||||
if (userSign.getMapOwn().size() > 1) {
|
||||
QuasarAppUtils::Params::log("Config signature contains more owners.",
|
||||
QuasarAppUtils::VerboseLvl::Warning);
|
||||
return fileSign;
|
||||
return upgradeOwner(userSign, fileSign);
|
||||
}
|
||||
|
||||
Signature signForSing = fileSign;
|
||||
if (userSign.getMapOwn().cbegin().value().getOwnerName() == signForSing.getMapOwn().cbegin().value().getOwnerName()) {
|
||||
|
||||
QMap<int, CopyrighFixer::Owner> mapOwners = signForSing.getMapOwn();
|
||||
mapOwners.remove(fileSign.getMapOwn().cbegin().key());
|
||||
mapOwners.insert(userSign.getMapOwn().cbegin().key(),
|
||||
userSign.getMapOwn().cbegin().value());
|
||||
signForSing.setMapOwners(mapOwners);
|
||||
|
||||
return signForSing;
|
||||
|
||||
if (userSign.getMapOwn().cbegin().value().getOwnerName() == fileSign.getMapOwn().cbegin().value().getOwnerName()) {
|
||||
return upgradeOwner(userSign, fileSign);
|
||||
}
|
||||
|
||||
if (userSign.getMapOwn().cbegin().value().getOwnerName() != signForSing.getMapOwn().cbegin().value().getOwnerName()) {
|
||||
|
||||
QMap<int, CopyrighFixer::Owner> mapOwners = signForSing.getMapOwn();
|
||||
mapOwners.insert(userSign.getMapOwn().cbegin().key(),
|
||||
userSign.getMapOwn().cbegin().value());
|
||||
signForSing.setMapOwners(mapOwners);
|
||||
|
||||
return signForSing;
|
||||
|
||||
if (userSign.getMapOwn().cbegin().value().getOwnerName() != fileSign.getMapOwn().cbegin().value().getOwnerName()) {
|
||||
return appendOwner(userSign, fileSign);
|
||||
}
|
||||
|
||||
return fileSign;
|
||||
|
||||
}
|
||||
|
||||
IFileManager *Signer::searchFileByExt(const QString &extension) {
|
||||
IFileManager *Signer::searchFileByExt(const QString &extension) const {
|
||||
|
||||
for (auto itemFM: _fileManager) {
|
||||
|
||||
if (itemFM && itemFM->isSupport(itemFM->toExtension(extension))) {
|
||||
return itemFM;
|
||||
}
|
||||
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -10,6 +10,7 @@
|
||||
|
||||
#include "CopyrighFixer_global.h"
|
||||
#include "config.h"
|
||||
#include <QFileInfo>
|
||||
|
||||
namespace CopyrighFixer {
|
||||
|
||||
@ -22,6 +23,8 @@ class CopyrighFixer_EXPORT Signer {
|
||||
public:
|
||||
Signer();
|
||||
|
||||
bool processSign(const QString &pathToFile, const Config &objConf) const;
|
||||
|
||||
/**
|
||||
* @brief checkSign The method that add copyright to all sources files.
|
||||
* @param objConf This is a configuration object.
|
||||
@ -32,7 +35,7 @@ public:
|
||||
* @brief mergeSign The method compares two signatures and merges into one.
|
||||
* @param userSign The signature that is pulled from the configuration.
|
||||
* @param fileSign The signature that is read from the file.
|
||||
* @return Returns the signature for signing a file.
|
||||
* @return Returns the signature for signing a filereturn false;.
|
||||
*/
|
||||
const Signature mergeSign(const Signature &userSign, const Signature &fileSign) const;
|
||||
|
||||
@ -41,11 +44,26 @@ public:
|
||||
* @param extension This is the file extension to search.
|
||||
* @return Returns a pointer to the FileManager of the found extension, or nullptr if the file extension was not found.
|
||||
*/
|
||||
IFileManager *searchFileByExt(const QString &extension);
|
||||
IFileManager *searchFileByExt(const QString &extension) const;
|
||||
|
||||
private:
|
||||
QList<IFileManager*> _fileManager;
|
||||
|
||||
/**
|
||||
* @brief upgradeOwner
|
||||
* @param signConf The signature that is pulled from the configuration.
|
||||
* @param fileSign The signature that is read from the file.
|
||||
* @return Returns a signature with the last owner updated.
|
||||
*/
|
||||
const Signature upgradeOwner(const Signature &signConf, const Signature &fileSign) const;
|
||||
|
||||
/**
|
||||
* @brief appendOwner
|
||||
* @param signConf The signature that is pulled from the configuration.
|
||||
* @param fileSign The signature that is read from the file.
|
||||
* @return Returns a signature with the most recently updated owner list.
|
||||
*/
|
||||
const Signature appendOwner(const Signature &signConf, const Signature &fileSign) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
//# of this license document, but changing it is not allowed.
|
||||
//#
|
||||
|
||||
#include <time.h>
|
||||
#include "signertest.h"
|
||||
#include <QDebug>
|
||||
|
||||
@ -61,12 +62,13 @@ CopyrighFixer::Signature SignerTest::genSign(QList<SignerTest::dataOwns> numOwns
|
||||
|
||||
QList<SignerTest::Signers> SignerTest::lstSing() const {
|
||||
|
||||
int unixTime = time(0);
|
||||
// 1
|
||||
// The case when the signatures in the config and the file match.
|
||||
SignerTest::Signers equalSign;
|
||||
equalSign.signFormConf = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1622657022);
|
||||
equalSign.signFormFile = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1622657022);
|
||||
equalSign.signAfterMerge = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1622657022);
|
||||
equalSign.signAfterMerge = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", unixTime);
|
||||
|
||||
// 2
|
||||
// The Different licenses.
|
||||
@ -80,10 +82,10 @@ QList<SignerTest::Signers> SignerTest::lstSing() const {
|
||||
SignerTest::Signers signDiffOwn;
|
||||
signDiffOwn.signFormConf = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1700000000);
|
||||
signDiffOwn.signFormFile = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarAppSdfdf", 1622657022);
|
||||
|
||||
signDiffOwn.signAfterMerge = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarAppSdfdf", 1622657022);
|
||||
|
||||
QMap<int, CopyrighFixer::Owner> multiSign = signDiffOwn.signAfterMerge.getMapOwn();
|
||||
multiSign.insert(1700000000, genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1700000000).getMapOwn().cbegin().value());
|
||||
multiSign.insert(unixTime, genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", unixTime).getMapOwn().cbegin().value());
|
||||
signDiffOwn.signAfterMerge.setMapOwners(multiSign);
|
||||
|
||||
// 4
|
||||
@ -91,7 +93,7 @@ QList<SignerTest::Signers> SignerTest::lstSing() const {
|
||||
SignerTest::Signers signEqualOwn;
|
||||
signEqualOwn.signFormConf = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1700000000);
|
||||
signEqualOwn.signFormFile = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1622657022);
|
||||
signEqualOwn.signAfterMerge = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1700000000);
|
||||
signEqualOwn.signAfterMerge = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", unixTime);
|
||||
|
||||
|
||||
// 5
|
||||
@ -126,7 +128,7 @@ QList<SignerTest::Signers> SignerTest::lstSing() const {
|
||||
SignerTest::Signers signDiffNumOwn;
|
||||
signDiffNumOwn.signFormConf = genSign(Owns, "MIT License Copyright (C) 2020-2021 QuasarApp.");
|
||||
signDiffNumOwn.signFormFile = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1700000000);
|
||||
signDiffNumOwn.signAfterMerge = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1700000000);
|
||||
signDiffNumOwn.signAfterMerge = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp1", unixTime);
|
||||
|
||||
return {equalSign, signDiffLic, signDiffOwn, signEqualOwn, signMissingInFile, signMissOwnInFile, signDiffNumOwn};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user