Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Commit

Permalink
Cypress in cli (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
janette authored Feb 8, 2022
1 parent cc2253b commit b715bab
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 405 deletions.
2 changes: 1 addition & 1 deletion assets/docker/docker-compose.common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ services:
# Used for all console commands and tools.
cli:
hostname: cli
image: getdkan/dkan-cli:v0.4.0
image: getdkan/dkan-cli:v0.4.2
env_file:
- "${DKTL_DIRECTORY}/assets/docker/mysql.env"
environment:
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"require": {
"consolidation/robo": "^3.0.0",
"pear/archive_tar": "^1.4",
"aws/aws-sdk-php": "^3.67",
"drupal/coder": "^8.3"
"aws/aws-sdk-php": "^3.67"
},
"autoload": {
"psr-4": {
Expand Down
523 changes: 193 additions & 330 deletions composer.lock

Large diffs are not rendered by default.

25 changes: 10 additions & 15 deletions src/Command/DkanCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DkanTools\Command;

use DkanTools\Util\Util;
use DkanTools\Util\TestUserTrait;

/**
* This is project's console commands configuration for Robo task runner.
Expand All @@ -12,6 +13,8 @@
*/
class DkanCommands extends \Robo\Tasks
{
use TestUserTrait;

/**
* Build DKAN docs with doxygen.
*/
Expand All @@ -30,12 +33,8 @@ public function dkanDocs()
*/
public function dkanTestCypress(array $args)
{
$this->dkanTestUser("testuser", "2jqzOAnXS9mmcLasy", "api_user");
$this->dkanTestUser("testeditor", "testeditor", "administrator");

$this->taskExec("npm install cypress")
->dir("docroot/modules/contrib/dkan")
->run();
$this->apiUser();
$this->editorUser();

$cypress = $this->taskExec('CYPRESS_baseUrl="http://$DKTL_PROXY_DOMAIN" npx cypress run')
->dir("docroot/modules/contrib/dkan");
Expand Down Expand Up @@ -172,6 +171,11 @@ public function dkanDemo()
->exec("dktl make")
->exec("dktl install")
->exec("dktl install:sample")
->exec("git clone -b "
. FrontendCommands::FRONTEND_VCS_REF
. " "
. FrontendCommands::FRONTEND_VCS_URL
. " " . FrontendCommands::FRONTEND_DIR)
->exec("dktl frontend:install")
->exec("dktl frontend:build")
->exec("dktl drush cr")
Expand Down Expand Up @@ -208,13 +212,4 @@ public function dkanDev()

$this->io()->success("Your dev site is available at: " . Util::getUri());
}

private function dkanTestUser($name, $pass, $roll)
{
$this->taskExecStack()
->stopOnFail()
->exec("dktl drush user:create $name --password=$pass")
->exec("dktl drush user-add-role $roll $name")
->run();
}
}
146 changes: 89 additions & 57 deletions src/Command/FrontendCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,35 @@ class FrontendCommands extends Tasks

const FRONTEND_DIR = 'src/frontend';
const FRONTEND_VCS_URL = 'https://github.com/GetDKAN/data-catalog-app/';
const FRONTEND_VCS_REF = 'master';
const FRONTEND_VCS_REF = 'cypress-update-8.7.0';
const FRONTEND_THEME = 'getdkan/dkan_js_frontend_bartik';


/**
* Download the DKAN frontend app to src/frontend.
*
* If no url or ref are provided, DKAN Tools will probe DKAN's composer.json
* file, looking for the following configuration:
*
* "extra": {
* "dkan-frontend": {
* "type": "vcs",
* "url": "https://github.com/GetDKAN/data-catalog-app",
* "ref": "1.0.0"
* }
* }
*
* If not found, the defaults set in self::FRONTEND_VCS_URL and
* self::FRONTEND_VCS_REF will be used.
*
* @option string type
* The type of frontend package. "vcs" currently the only type supported.
* @option string url
* URL for the frontend package. Currently only github URLs supported.
* @option string ref
* Reference (tag, branch or commit) from the vcs system to use.
*
*/
* Download the DKAN frontend app to src/frontend.
*
* If no url or ref are provided, DKAN Tools will probe DKAN's composer.json
* file, looking for the following configuration:
*
* "extra": {
* "dkan-frontend": {
* "type": "vcs",
* "url": "https://github.com/GetDKAN/data-catalog-app",
* "ref": "1.0.0"
* }
* }
*
* If not found, the defaults set in self::FRONTEND_VCS_URL and
* self::FRONTEND_VCS_REF will be used.
*
* @option string type
* The type of frontend package. "vcs" currently the only type supported.
* @option string url
* URL for the frontend package. Currently only github URLs supported.
* @option string ref
* Reference (tag, branch or commit) from the vcs system to use.
*
*/
public function frontendGet($opts = ['type' => 'vcs', 'url' => null, 'ref' => null])
{
if (file_exists(self::FRONTEND_DIR)) {
Expand Down Expand Up @@ -79,7 +80,7 @@ private function frontendGetPopulateDefaults(&$opts)
if ($opts['url'] && $opts['ref']) {
return;
}
$defaults = ['url' => self::FRONTEND_VCS_URL, 'ref' => self::FRONTEND_VCS_REF ];
$defaults = ['url' => self::FRONTEND_VCS_URL, 'ref' => self::FRONTEND_VCS_REF];
$note = "Frontend config not found in DKAN composer.json. Reverting to "
. "defaults from DKAN Tools";

Expand Down Expand Up @@ -130,13 +131,13 @@ private function getVcsArchiveUrl($url, $ref)
}

/**
* Create symlink for src/frontend.
*/
* Create symlink for src/frontend.
*/
private function frontendLink()
{
$result = $this->taskExec('ln -s ../src/frontend frontend')
->dir("docroot")
->run();
->dir("docroot")
->run();
if ($result && $result->getExitCode() === 0) {
$this->io()->success(
'Successfully symlinked /src/frontend to docroot/frontend'
Expand All @@ -145,18 +146,18 @@ private function frontendLink()
}

/**
* Download frontend app if not present, and run npm install.
*
* The URL and branch/tag for the frontend app should be specified in the
* "extra" section of DKAN's composer.json. If you want to specify a
* different tag or branch, or different repo entirely, run "dktl
* frontend:get" first and specify the --ref and/or --url options.
*
* @param array $opts
* Options array.
* @option theme
* Whether or not to install default front-end theme. Defaults to true.
*/
* Download frontend app if not present, and run npm install.
*
* The URL and branch/tag for the frontend app should be specified in the
* "extra" section of DKAN's composer.json. If you want to specify a
* different tag or branch, or different repo entirely, run "dktl
* frontend:get" first and specify the --ref and/or --url options.
*
* @param array $opts
* Options array.
* @option theme
* Whether or not to install default front-end theme. Defaults to true.
*/
public function frontendInstall($opts = ['theme' => true])
{
if (!file_exists(self::FRONTEND_DIR)) {
Expand All @@ -165,6 +166,35 @@ public function frontendInstall($opts = ['theme' => true])
if (!file_exists("docroot/frontend")) {
$this->frontendLink();
}
$this->cypressLink();
$this->npmInstall();
$this->moduleInstall();

if ($opts['theme']) {
$this->installTheme();
}

$this->taskExec("drush config-set system.site page.front \"/home\" -y")->run();
$this->io()->success('Set front page.');
}

/**
* Install DKAN frontend Drupal module.
*/
private function moduleInstall()
{
$result = $this->taskExec("drush en -y dkan_js_frontend")->dir("docroot")->run();
if ($result->getExitCode() != 0) {
$this->io()->error('Could not install front-end node module');
return $result;
}
}

/**
* Install frontend npm dependencies.
*/
private function npmInstall()
{
$result = $this->taskExec("npm install")
->dir("src/frontend")
->run();
Expand All @@ -173,18 +203,24 @@ public function frontendInstall($opts = ['theme' => true])
return $result;
}
$this->io()->success('Front-end dependencies installed.');
$result = $this->taskExec("drush en -y dkan_js_frontend")->dir("docroot")->run();
if ($result->getExitCode() != 0) {
$this->io()->error('Could not install front-end node module');
}

/**
* Symlink in global cypress installation.
*/
private function cypressLink()
{
$result = $this->taskExec("npm link ../../../../usr/local/bin/node_modules/cypress")
->dir("src/frontend")
->run();
if ($result && $result->getExitCode() === 0) {
$this->io()->success(
'Successfully symlinked global cypress into frontend folder.'
);
} else {
$this->io()->error('Could not symlink package folder');
return $result;
}

if ($opts['theme']) {
$this->installTheme();
}

$this->taskExec("drush config-set system.site page.front \"/home\" -y")->run();
$this->io()->success('Set front page.');
}

/**
Expand Down Expand Up @@ -225,7 +261,7 @@ public function frontendBuild()
{
// Override GATSBY_API_URL with our own proxied domain.
$task = $this
->taskExec('npm run build')
->taskExec('npm run build --force')
->dir("src/frontend");
$result = $task->run();
if ($result->getExitCode() != 0) {
Expand All @@ -245,10 +281,6 @@ private function frontendModulePresent()
*/
public function frontendTest()
{
$this->taskExec("npm install cypress")
->dir("docroot/frontend")
->run();

return $this->taskExec('CYPRESS_baseUrl="http://$DKTL_PROXY_DOMAIN" npx cypress run')
->dir("docroot/frontend")
->run();
Expand Down
95 changes: 95 additions & 0 deletions src/Command/ProjectCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace DkanTools\Command;

use DkanTools\Util\Util;
use DkanTools\Util\TestUserTrait;

/**
* This project's console commands configuration for Robo task runner.
*
* @see http://robo.li/
*/
class ProjectCommands extends \Robo\Tasks
{
use TestUserTrait;

/**
* Run project cypress tests.
*/
public function projectTestCypress(array $args)
{
$this->apiUser();
$this->editorUser();

$result = $this->taskExec("npm link ../../../../usr/local/bin/node_modules/cypress")
->dir("src/tests")
->run();
if ($result->getExitCode() != 0) {
$this->io()->error('Could not symlink package folder');
return $result;
}

$task = $this
->taskExec('npm install --force')
->dir("src/tests");
$result = $task->run();
if ($result->getExitCode() != 0) {
$this->io()->error('Could not insall test dependencies.');
return $result;
}
$this->io()->success('Installation of test dependencies successful.');

$cypress = $this->taskExec('CYPRESS_baseUrl="http://$DKTL_PROXY_DOMAIN" npx cypress run')
->dir("src/tests");

foreach ($args as $arg) {
$cypress->arg($arg);
}

return $cypress->run();
}

/**
* Run Site PhpUnit Tests. Additional phpunit CLI options can be passed.
*
* @see https://phpunit.de/manual/6.5/en/textui.html#textui.clioptions
* @param array $args Arguments to append to phpunit command.
*/
public function projectTestPhpunit(array $args)
{
$proj_dir = Util::getProjectDirectory();
$phpunit_executable = $this->getPhpUnitExecutable();

$phpunitExec = $this->taskExec($phpunit_executable)
->option('testsuite', 'Custom Test Suite')
->dir("{$proj_dir}/docroot/modules/custom");

foreach ($args as $arg) {
$phpunitExec->arg($arg);
}

return $phpunitExec->run();
}

private function getPhpUnitExecutable()
{
$proj_dir = Util::getProjectDirectory();

$phpunit_executable = $phpunit_executable = "{$proj_dir}/vendor/bin/phpunit";

if (!file_exists($phpunit_executable)) {
$this->taskExec("dktl installphpunit")->run();
$phpunit_executable = "phpunit";
}

return $phpunit_executable;
}

private function inGitDetachedState($dkanDirPath)
{
$output = [];
exec("cd {$dkanDirPath} && git rev-parse --abbrev-ref HEAD", $output);
return (isset($output[0]) && $output[0] == 'HEAD');
}
}
Loading

0 comments on commit b715bab

Please sign in to comment.