FOSSology  4.4.0
Open Source License Compliance by Open Source Software
CommonCliTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
16 
22 class CommonCliTest extends \PHPUnit\Framework\TestCase
23 {
24 
29  protected $testDb;
30 
35  protected $testInstaller;
36 
41  protected $agentDir;
42 
47  protected function setUp() : void
48  {
49  $this->testDb = new TestPgDb("nomosfun" . time());
50  $this->agentDir = dirname(__DIR__, 4).'/build/src/nomos';
51  $this->testdir = dirname(dirname(__DIR__)) .
52  "/agent_tests/testdata/NomosTestfiles/";
53 
54  $sysConf = $this->testDb->getFossSysConf();
55  $this->testInstaller = new TestInstaller($sysConf);
56  $this->testInstaller->init();
57  $this->testInstaller->install($this->agentDir);
58 
59  $this->testDb->createSequences(array(
60  'license_ref_rf_pk_seq'
61  ), false);
62  $this->testDb->createPlainTables(array(
63  'agent',
64  'license_ref'
65  ), false);
66  $this->testDb->alterTables(array(
67  'license_ref'
68  ), false);
69  }
70 
75  protected function tearDown() : void
76  {
77  $this->testInstaller->uninstall($this->agentDir);
78  $this->testInstaller->clear();
79  $this->testInstaller->rmRepo();
80  $this->testDb = null;
81  }
82 
92  protected function runNomos($args = "", $files = array())
93  {
94  $sysConf = $this->testDb->getFossSysConf();
95 
96  $confFile = $sysConf . "/fossology.conf";
97  system("touch " . $confFile);
98  $config = "[FOSSOLOGY]\ndepth = 0\npath = $sysConf/repo\n";
99  file_put_contents($confFile, $config);
100 
101  $execDir = $this->agentDir . '/agent';
102  system(
103  "install -D $this->agentDir/VERSION $sysConf/mods-enabled/nomos/VERSION");
104 
105  foreach ($files as $file) {
106  $args .= " " . escapeshellarg($file);
107  }
108 
109  $pipeFd = popen("$execDir/nomos -c $sysConf $args", "r");
110  $this->assertTrue($pipeFd !== false, 'running nomos failed');
111 
112  $output = "";
113  while (($buffer = fgets($pipeFd, 4096)) !== false) {
114  $output .= $buffer;
115  }
116  $retCode = pclose($pipeFd);
117 
118  unlink("$sysConf/mods-enabled/nomos/VERSION");
119  // unlink("$sysConf/mods-enabled/nomos");
120  // rmdir("$sysConf/mods-enabled");
121  unlink($confFile);
122 
123  return array(
124  $output,
125  $retCode
126  );
127  }
128 
135  public function testHelp()
136  {
137  $nomos = dirname(__DIR__, 4) . '/build/src/nomos/agent/nomos';
138  list ($output,) = $this->runNomos($args = "-h"); // exec("$nomos -h 2>&1",
139  // $out, $rtn);
140  $out = explode("\n", $output);
141  $usage = "Usage: $nomos [options] [file [file [...]]";
142  $this->assertEquals($usage, $out[0]);
143  }
144 }
Tests for common CLI operations.
setUp()
Setup the test cases and initialize the objects.
tearDown()
Destruct the objects initialized during setUp()
runNomos($args="", $files=array())
Run nomos using the arguments passed.
testHelp()
Test for nomos help message.