31 public static function providerWhetherToUseStandalone()
39 protected function setUp()
41 $this->testDataDir = dirname(__DIR__).
"/testlicenses";
42 $this->testDb =
new TestPgDb(
"monkCli".time());
43 $this->
dbManager = $this->testDb->getDbManager();
46 protected function tearDown()
48 $this->testDb->fullDestruct();
53 private function runMonk($args=
"", $files=array(), $standalone=FALSE)
56 $temporaryKB = tempnam(
"/tmp",
"monk.knowledgebase");
57 list($output,$retCode) = $this->runMonk(
"-s $temporaryKB");
58 $this->assertEquals(0, $retCode,
"monk failed to save the knowledgebase to $temporaryKB: ".$output);
59 $result = $this->runMonk(
"-k $temporaryKB $args", $files);
63 $sysConf = $this->testDb->getFossSysConf();
65 $confFile = $sysConf.
"/fossology.conf";
66 system(
"touch ".$confFile);
67 $config =
"[FOSSOLOGY]\ndepth = 0\npath = $sysConf/repo\n";
68 file_put_contents($confFile, $config);
72 $agentDir = dirname(dirname(__DIR__));
74 system(
"install -D $agentDir/VERSION-monk $sysConf/mods-enabled/$agentName/VERSION");
76 foreach ($files as $file) {
77 $args .=
" ".escapeshellarg($file);
80 $cmd =
"$execDir/$agentName -c $sysConf $args";
81 $pipeFd = popen($cmd,
"r");
82 $this->assertTrue($pipeFd !==
false,
'running monk failed');
85 while (($buffer = fgets($pipeFd, 4096)) !==
false) {
88 $retCode = pclose($pipeFd);
91 unlink(
"$sysConf/mods-enabled/$agentName/VERSION");
92 rmdir(
"$sysConf/mods-enabled/$agentName");
93 rmdir(
"$sysConf/mods-enabled");
96 return array($output,$retCode);
99 private function setUpTables()
101 $this->testDb->createPlainTables(array(
'license_ref'),
false);
102 $this->testDb->createSequences(array(
'license_ref_rf_pk_seq'),
false);
104 $this->testDb->insertData_license_ref(1<<10);
112 $this->setUpTables();
114 list($output,$retCode) = $this->runMonk(
"", array($this->testDataDir.
"/expectedFull/Apache-2.0"), $standalone);
116 $this->assertEquals(0, $retCode,
'monk failed: '.$output);
118 $this->assertRegExp(
"/found full match between \".*expectedFull\\/Apache-2.0\" and \"Apache-2\\.0\" \\(rf_pk=[0-9]*\\); matched: 0\\+10456\n/", $output);
121 private function extractSortedLines($output) {
122 $lines = explode(
"\n", $output);
124 sort($lines, SORT_STRING);
125 foreach($lines as $key => $val) {
131 return array_values($lines);
134 private function assertLinesRegex($regexFmt, $lines, $testFiles) {
135 for ($i = 0; $i < count($lines); $i++)
139 $file = $testFiles[$i];
140 $licenseName = preg_quote(preg_replace(
'/.*\/([^,]*),?[^,]*/',
'${1}', $file),
"/");
142 $fileName = preg_quote($file,
"/");
145 $regex = preg_replace(
"/\\\$fileName/", $fileName, $regex);
146 $regex = preg_replace(
"/\\\$licenseName/", $licenseName, $regex);
148 $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 $this->assertRegExp($expectedOutputRgx,$output);
220 $this->setUpTables();
222 $fileName = tempnam(
".",
"monkCli");
224 list($output,$retCode) = $this->runMonk(
"", array($fileName), $standalone);
228 $this->assertEquals(0, $retCode,
'monk failed: '.$output);
230 $this->assertEquals(
"",$output);
238 $this->setUpTables();
240 $fileName = tempnam(
".",
"monkCli");
241 $testFiles = array($fileName);
243 list($output,$retCode) = $this->runMonk(
"-v", $testFiles, $standalone);
247 $this->assertEquals(0, $retCode,
'monk failed: '.$output);
249 sort($testFiles, SORT_STRING);
250 $lines = $this->extractSortedLines($output);
252 $this->assertEquals(count($testFiles), count($lines),
253 "scanned\n".implode(
"\n",$testFiles).
"\n---\noutput\n".implode(
"\n",$lines).
"\n---\n");
255 $this->assertLinesRegex(
'/$fileName contains license\(s\) No_license_found/',
testRunMultipleMonkScansFulls($standalone)
testRunMonkHelpMode($standalone)
testRunMultipleMonkScansDiff($standalone)
testRunMonkScansWithNegativeMatchVerbose($standalone)
testRunMonkScan($standalone)
fo_dbManager * dbManager
fo_dbManager object
testRunMonkScansWithNegativeMatch($standalone)