10 use PHPUnit\Runner\Version as PHPUnitVersion;
12 class cliTest extends \PHPUnit\Framework\TestCase
21 public static function providerWhetherToUseStandalone()
29 protected function setUp() :
void
31 $this->testDataDir = dirname(__DIR__).
"/testlicenses";
32 $this->testDb =
new TestPgDb(
"monkCli".time());
33 $this->
dbManager = $this->testDb->getDbManager();
36 protected function tearDown() :
void
38 $this->testDb->fullDestruct();
43 private function runMonk($args=
"", $files=array(), $standalone=FALSE)
46 $temporaryKB = tempnam(
"/tmp",
"monk.knowledgebase");
47 list($output,$retCode) = $this->runMonk(
"-s $temporaryKB");
48 $this->assertEquals(0, $retCode,
"monk failed to save the knowledgebase to $temporaryKB: ".$output);
49 $result = $this->runMonk(
"-k $temporaryKB $args", $files);
53 $sysConf = $this->testDb->getFossSysConf();
55 $confFile = $sysConf.
"/fossology.conf";
56 system(
"touch ".$confFile);
57 $config =
"[FOSSOLOGY]\ndepth = 0\npath = $sysConf/repo\n";
58 file_put_contents($confFile, $config);
62 $agentDir = dirname(__DIR__,4).
'/build/src/monk';
63 $execDir = $agentDir.
'/agent';
64 system(
"install -D $agentDir/VERSION-monk $sysConf/mods-enabled/$agentName/VERSION");
66 foreach ($files as $file) {
67 $args .=
" ".escapeshellarg($file);
70 $cmd =
"$execDir/$agentName -c $sysConf $args";
71 $pipeFd = popen($cmd,
"r");
72 $this->assertTrue($pipeFd !==
false,
'running monk failed');
75 while (($buffer = fgets($pipeFd, 4096)) !==
false) {
78 $retCode = pclose($pipeFd);
81 unlink(
"$sysConf/mods-enabled/$agentName/VERSION");
82 rmdir(
"$sysConf/mods-enabled/$agentName");
83 rmdir(
"$sysConf/mods-enabled");
86 return array($output,$retCode);
89 private function setUpTables()
91 $this->testDb->createPlainTables(array(
'license_ref'),
false);
92 $this->testDb->createSequences(array(
'license_ref_rf_pk_seq'),
false);
93 $this->testDb->alterTables(array(
'license_ref'),
false);
95 $this->testDb->insertData_license_ref(1<<10);
103 $this->setUpTables();
105 list($output,$retCode) = $this->runMonk(
"", array($this->testDataDir.
"/expectedFull/Apache-2.0"), $standalone);
107 $this->assertEquals(0, $retCode,
'monk failed: '.$output);
109 $pattern =
"/found full match between \".*expectedFull\\/Apache-2.0\" and \"Apache-2\\.0\" \\(rf_pk=[0-9]*\\); matched: 0\\+10456\n/";
110 if (intval(explode(
'.', PHPUnitVersion::id())[0]) >= 9) {
111 $this->assertMatchesRegularExpression($pattern, $output);
113 $this->assertRegExp($pattern, $output);
117 private function extractSortedLines($output) {
118 $lines = explode(
"\n", $output);
120 sort($lines, SORT_STRING);
121 foreach($lines as $key => $val) {
127 return array_values($lines);
130 private function assertLinesRegex($regexFmt, $lines, $testFiles) {
131 for ($i = 0; $i < count($lines); $i++)
135 $file = $testFiles[$i];
136 $licenseName = preg_quote(preg_replace(
'/.*\/([^,]*),?[^,]*/',
'${1}', $file),
"/");
138 $fileName = preg_quote($file,
"/");
141 $regex = preg_replace(
"/\\\$fileName/", $fileName, $regex);
142 $regex = preg_replace(
"/\\\$licenseName/", $licenseName, $regex);
144 if (intval(explode(
'.', PHPUnitVersion::id())[0]) >= 9) {
145 $this->assertMatchesRegularExpression($regex, $line);
147 $this->assertRegExp($regex, $line);
157 $this->setUpTables();
159 $testFiles = glob($this->testDataDir.
"/expectedFull/*");
161 list($output,$retCode) = $this->runMonk(
"", $testFiles, $standalone);
163 $this->assertEquals(0, $retCode,
'monk failed: '.$output);
165 sort($testFiles, SORT_STRING);
166 $lines = $this->extractSortedLines($output);
168 $this->assertEquals(count($testFiles), count($lines),
169 "scanned\n".implode(
"\n",$testFiles).
"\n---\noutput\n".implode(
"\n",$lines).
"\n---\n");
172 $this->assertLinesRegex(
'/found full match between "$fileName" and "$licenseName" \(rf_pk=[0-9]+\); matched: [0-9]+\+[0-9]+/',
181 $this->setUpTables();
183 $testFiles = glob($this->testDataDir.
"/expectedDiff/*");
185 list($output,$retCode) = $this->runMonk(
"", $testFiles, $standalone);
187 $this->assertEquals(0, $retCode,
'monk failed: '.$output);
189 sort($testFiles, SORT_STRING);
190 $lines = $this->extractSortedLines($output);
192 $this->assertEquals(count($testFiles), count($lines),
193 "scanned\n".implode(
"\n",$testFiles).
"\n---\noutput\n".implode(
"\n",$lines).
"\n---\n");
195 $this->assertLinesRegex(
'/found diff match between "$fileName" and "$licenseName" \(rf_pk=[0-9]+\); rank [0-9]{1,3}; diffs: \{[stMR+\[\]0-9, -]+\}/',
204 $this->setUpTables();
206 list($output,$retCode) = $this->runMonk(
"-h", array(), $standalone);
208 $this->assertEquals(0, $retCode,
'monk failed: '.$output);
210 $expectedOutputRgx =
'/Usage:.*/';
212 if (intval(explode(
'.', PHPUnitVersion::id())[0]) >= 9) {
213 $this->assertMatchesRegularExpression($expectedOutputRgx, $output);
215 $this->assertRegExp($expectedOutputRgx, $output);
224 $this->setUpTables();
226 $fileName = tempnam(
".",
"monkCli");
228 list($output,$retCode) = $this->runMonk(
"", array($fileName), $standalone);
232 $this->assertEquals(0, $retCode,
'monk failed: '.$output);
234 $this->assertEquals(
"",$output);
242 $this->setUpTables();
244 $fileName = tempnam(
".",
"monkCli");
245 $testFiles = array($fileName);
247 list($output,$retCode) = $this->runMonk(
"-v", $testFiles, $standalone);
251 $this->assertEquals(0, $retCode,
'monk failed: '.$output);
253 sort($testFiles, SORT_STRING);
254 $lines = $this->extractSortedLines($output);
256 $this->assertEquals(count($testFiles), count($lines),
257 "scanned\n".implode(
"\n",$testFiles).
"\n---\noutput\n".implode(
"\n",$lines).
"\n---\n");
259 $this->assertLinesRegex(
'/$fileName contains license\(s\) No_license_found/',
testRunMultipleMonkScansFulls($standalone)
testRunMultipleMonkScansDiff($standalone)
testRunMonkScan($standalone)
testRunMonkHelpMode($standalone)
testRunMonkScansWithNegativeMatchVerbose($standalone)
testRunMonkScansWithNegativeMatch($standalone)
fo_dbManager * dbManager
fo_dbManager object