chore: fix comments, remove and add some code for karma

This commit is contained in:
salman2013
2023-10-02 16:04:33 +05:00
parent 3ecf2b9a55
commit d668af2fce
10 changed files with 511 additions and 65 deletions

View File

@@ -78,7 +78,7 @@ class StubLtiHandler(StubHttpRequestHandler):
'callback_url': self.post_dict.get('lis_outcome_service_url').replace('https', 'http'),
'sourcedId': self.post_dict.get('lis_result_sourcedid')
}
host = os.environ.get(self.server.server_address[0])
host = self.server.server_address[0]
submit_url = f'//{host}:{self.server.server_address[1]}'
content = self._create_content(status_message, submit_url)
self.send_response(200, content)
@@ -296,7 +296,7 @@ class StubLtiHandler(StubHttpRequestHandler):
"""
client_secret = str(self.server.config.get('client_secret', self.DEFAULT_CLIENT_SECRET))
host = os.environ.get('127.0.0.1')
host = '127.0.0.1'
port = self.server.server_address[1]
lti_base = self.DEFAULT_LTI_ADDRESS.format(host=host, port=port)
lti_endpoint = self.server.config.get('lti_endpoint', self.DEFAULT_LTI_ENDPOINT)

View File

@@ -0,0 +1,12 @@
/* eslint-env node */
'use strict';
// By default, fixtures are loaded from spec/javascripts/fixtures but in karma everything gets served from /base
jasmine.getFixtures().fixturesPath = '/base/';
// https://github.com/edx/js-test-tool/blob/master/js_test_tool/templates/jasmine_test_runner.html#L10
// Stub out modal dialog alerts, which will prevent
// us from accessing the test results in the DOM
window.confirm = function() { return true; };
window.alert = function() { };

View File

@@ -0,0 +1,29 @@
/* This file overrides ExceptionFormatter of jasmine before it's initialization in karma-jasmine's
boot.js. It's important because ExceptionFormatter returns a constructor function. Once the method has been
initialized we can't override the ExceptionFormatter as Jasmine then uses the stored reference to the function */
(function() {
/* globals jasmineRequire */
'use strict';
var OldExceptionFormatter = jasmineRequire.ExceptionFormatter(),
oldExceptionFormatter = new OldExceptionFormatter(),
MAX_STACK_TRACE_LINES = 10;
jasmineRequire.ExceptionFormatter = function() {
function ExceptionFormatter() {
this.message = oldExceptionFormatter.message;
this.stack = function(error) {
var errorMsg = null;
if (error) {
errorMsg = error.stack.split('\n').slice(0, MAX_STACK_TRACE_LINES).join('\n');
}
return errorMsg;
};
}
return ExceptionFormatter;
};
}());

View File

@@ -0,0 +1,415 @@
// Common settings and helpers for setting up Karma config.
//
// To run all the tests in a suite and print results to the console:
//
// karma start <karma_config_for_suite_path>
// E.g. karma start lms/static/karma_lms.conf.js
//
//
// To run the tests for debugging: Debugging can be done in any browser
// but Chrome's developer console debugging experience is best.
//
// karma start <karma_config_for_suite_path> --browsers=BROWSER --single-run=false
//
//
// To run the tests with coverage and junit reports:
//
// karma start <karma_config_for_suite_path> --browsers=BROWSER
// --coverage --junitreportpath=<xunit_report_path> --coveragereportpath=<report_path>
//
// where `BROWSER` could be Chrome or Firefox.
//
//
// Troubleshooting tips:
//
// If you get an error like: "TypeError: __cov_KBCc7ZI4xZm8W2BC5NQLDg.s is undefined",
// that means the patterns in sourceFiles and specFiles are matching the same file.
// This causes Istanbul, which is used for tracking coverage to instrument the file
// multiple times.
//
//
// If you see the error: "EMFILE, too many open files" that means the files pattern
// that has been added is matching too many files. The glob library used by Karma
// does not use graceful-fs and tries to read files simultaneously.
//
/* eslint-env node */
/* globals process */
'use strict';
var path = require('path');
var _ = require('underscore');
var appRoot = path.join(__dirname, '../../../../');
// eslint-disable-next-line import/no-extraneous-dependencies
var webdriver = require('selenium-webdriver');
// eslint-disable-next-line import/no-extraneous-dependencies
var webpackConfig = require(path.join(appRoot, 'webpack.dev.config.js'));
// The following crazy bit is to work around the webpack.optimize.CommonsChunkPlugin
// plugin. The problem is that it it factors out the code that defines webpackJsonp
// and puts in in the commons JS, which Karma doesn't know to load first. This is a
// workaround recommended in the karma-webpack bug report that basically just removes
// the plugin for the purposes of Karma testing (the plugin is meant to be an
// optimization only).
// https://github.com/webpack-contrib/karma-webpack/issues/24#issuecomment-257613167
//
// This should be fixed in v3 of karma-webpack
var commonsChunkPluginIndex = webpackConfig[0].plugins.findIndex(function(plugin) { return plugin.chunkNames; });
// Files which are needed by all lms/cms suites.
var commonFiles = {
libraryFiles: [
{pattern: 'common/js/vendor/**/*.js'},
{pattern: 'edx-ui-toolkit/js/**/*.js'},
{pattern: 'xmodule_js/common_static/common/js/**/!(*spec).js'},
{pattern: 'xmodule_js/common_static/js/**/!(*spec).js'},
{pattern: 'xmodule_js/src/**/*.js'}
],
sourceFiles: [
{pattern: 'common/js/!(spec_helpers)/**/!(*spec).js'}
],
specFiles: [
{pattern: 'common/js/spec_helpers/**/*.js'}
],
fixtureFiles: [
{pattern: 'common/templates/**/*.underscore'}
]
};
webpackConfig[0].plugins.splice(commonsChunkPluginIndex, 1);
delete webpackConfig[0].entry;
/**
* Customize the name attribute in xml testcase element
* @param {Object} browser
* @param {Object} result
* @return {String}
*/
function junitNameFormatter(browser, result) {
return result.suite[0] + ': ' + result.description;
}
/**
* Customize the classname attribute in xml testcase element
* @param {Object} browser
* @return {String}
*/
function junitClassNameFormatter(browser) {
return 'Javascript.' + browser.name.split(' ')[0];
}
/**
* Return array containing default and user supplied reporters
* @param {Object} config
* @return {Array}
*/
function reporters(config) {
var defaultReporters = ['spec', 'junit', 'kjhtml'];
if (config.coverage) {
defaultReporters.push('coverage');
}
return defaultReporters;
}
/**
* Split a filepath into basepath and filename
* @param {String} filepath
* @return {Object}
*/
function getBasepathAndFilename(filepath) {
var file, dir;
if (!filepath) {
// these will configure the reporters to create report files relative to this karma config file
return {
dir: undefined,
file: undefined
};
}
file = filepath.replace(/^.*[\\/]/, '');
dir = filepath.replace(file, '');
return {
dir: dir,
file: file
};
}
/**
* Return coverage reporter settings
* @param {String} config
* @return {Object}
*/
function coverageSettings(config) {
var pth = getBasepathAndFilename(config.coveragereportpath);
return {
dir: pth.dir,
subdir: '.',
includeAllSources: true,
reporters: [
{type: 'cobertura', file: pth.file},
{type: 'text-summary'}
]
};
}
/**
* Return junit reporter settings
* @param {String} config
* @return {Object}
*/
function junitSettings(config) {
var pth = getBasepathAndFilename(config.junitreportpath);
return {
outputDir: pth.dir,
outputFile: pth.file,
suite: 'javascript',
useBrowserName: false,
nameFormatter: junitNameFormatter,
classNameFormatter: junitClassNameFormatter
};
}
/**
* Return absolute path for files in common and xmodule_js symlink dirs.
* @param {String} appRoot
* @param {String} pattern
* @return {String}
*/
// I'd like to fix the no-shadow violation on the next line, but it would break this shared conf's API.
function defaultNormalizeFunc(appRoot, pattern) { // eslint-disable-line no-shadow
var pat = pattern;
if (pat.match(/^common\/js/)) {
pat = path.join(appRoot, '/common/static/' + pat);
} else if (pat.match(/^xmodule_js\/common_static/)) {
pat = path.join(appRoot, '/common/static/'
+ pat.replace(/^xmodule_js\/common_static\//, ''));
}
return pat;
}
function normalizePathsForCoverage(files, normalizeFunc, preprocessors) {
var normalizeFn = normalizeFunc || defaultNormalizeFunc,
normalizedFile,
filesForCoverage = {};
files.forEach(function(file) {
if (!file.ignoreCoverage) {
normalizedFile = normalizeFn(appRoot, file.pattern);
if (preprocessors && preprocessors.hasOwnProperty(normalizedFile)) {
filesForCoverage[normalizedFile] = ['coverage'].concat(preprocessors[normalizedFile]);
} else {
filesForCoverage[normalizedFile] = ['coverage'];
}
}
});
return filesForCoverage;
}
/**
* Sets defaults for each file pattern.
* RequireJS files are excluded by default.
* Webpack files are included by default.
* @param {Object} files
* @return {Object}
*/
function setDefaults(files) {
return files.map(function(f) {
var file = _.isObject(f) ? f : {pattern: f};
if (!file.included && !file.webpack) {
file.included = false;
}
return file;
});
}
function getBaseConfig(config, useRequireJs) {
var getFrameworkFiles = function() {
var files = [
'common/static/common/js/vendor/jquery.js',
'node_modules/jasmine-core/lib/jasmine-core/jasmine.js',
'common/static/common/js/jasmine_stack_trace.js',
'node_modules/karma-jasmine/lib/boot.js',
'node_modules/karma-jasmine/lib/adapter.js',
'node_modules/jasmine-jquery/lib/jasmine-jquery.js',
'node_modules/popper.js/dist/umd/popper.js',
'node_modules/bootstrap/dist/js/bootstrap.js',
'node_modules/underscore/underscore.js',
'node_modules/backbone/backbone.js',
'common/static/js/test/i18n.js'
];
if (useRequireJs) {
files = files.concat([
'node_modules/requirejs/require.js',
'node_modules/karma-requirejs/lib/adapter.js'
]);
}
return files;
};
// Manually prepends the framework files to the karma files array
// bypassing the karma's framework config. This is necessary if you want
// to add a library or framework that isn't a karma plugin. e.g. we add jasmine-jquery
// which isn't a karma plugin. Though a karma framework for jasmine-jquery is available
// but it's not actively maintained. In future we also wanna add jQuery at the top when
// we upgrade to jQuery 2
var initFrameworks = function(files) {
getFrameworkFiles().reverse().forEach(function(f) {
files.unshift({
pattern: path.join(appRoot, f),
included: true,
served: true,
watch: false
});
});
};
var hostname = 'localhost';
var port = 9876;
var customPlugin = {
'framework:custom': ['factory', initFrameworks]
};
initFrameworks.$inject = ['config.files'];
return {
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['custom'],
plugins: [
'karma-jasmine',
'karma-jasmine-html-reporter',
'karma-requirejs',
'karma-junit-reporter',
'karma-coverage',
'karma-spec-reporter',
'karma-selenium-webdriver-launcher',
'karma-webpack',
'karma-sourcemap-loader',
customPlugin
],
// list of files to exclude
exclude: [],
// karma-reporter
reporters: reporters(config),
// Spec Reporter configuration
specReporter: {
maxLogLines: 5,
showSpecTiming: true
},
coverageReporter: coverageSettings(config),
junitReporter: junitSettings(config),
// web server hostname and port
hostname: hostname,
port: port,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
/* possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN
|| config.LOG_INFO || config.LOG_DEBUG */
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: config.singleRun,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
browserNoActivityTimeout: 50000,
client: {
captureConsole: false
},
webpack: webpackConfig[0],
webpackMiddleware: {
watchOptions: {
poll: true
}
}
};
}
function configure(config, options) {
var useRequireJs = options.useRequireJs === undefined ? true : options.useRequireJs,
baseConfig = getBaseConfig(config, useRequireJs),
files, filesForCoverage, preprocessors;
if (options.includeCommonFiles) {
_.forEach(['libraryFiles', 'sourceFiles', 'specFiles', 'fixtureFiles'], function(collectionName) {
options[collectionName] = _.flatten([commonFiles[collectionName], options[collectionName]]);
});
}
files = _.flatten(
_.map(
['libraryFilesToInclude', 'libraryFiles', 'sourceFiles', 'specFiles', 'fixtureFiles', 'runFiles'],
function(collectionName) { return options[collectionName] || []; }
)
);
files.unshift(
{pattern: path.join(appRoot, 'common/static/common/js/jasmine.common.conf.js'), included: true}
);
if (useRequireJs) {
files.unshift({pattern: 'common/js/utils/require-serial.js', included: true});
}
// Karma sets included=true by default.
// We set it to false by default because RequireJS should be used instead.
files = setDefaults(files);
filesForCoverage = _.flatten(
_.map(
['sourceFiles', 'specFiles'],
function(collectionName) { return options[collectionName]; }
)
);
// If we give symlink paths to Istanbul, coverage for each path gets tracked
// separately. So we pass absolute paths to the karma-coverage preprocessor.
preprocessors = _.extend(
{},
options.preprocessors,
normalizePathsForCoverage(filesForCoverage, options.normalizePathsForCoverageFunc, options.preprocessors)
);
config.set(_.extend(baseConfig, {
files: files,
preprocessors: preprocessors
}));
}
module.exports = {
configure: configure,
appRoot: appRoot
};

View File

@@ -58,6 +58,7 @@ and for all environments to use __init__.py to load their settings.
The following files should be obviated by this change:
* bok_choy_docker.py
* devstack_docker.py
* devstack_optimized.py
* devstack.py

View File

@@ -228,7 +228,7 @@ class TestProblemTypeAccess(SharedModuleStoreTestCase, MasqueradeMixin): # pyli
graded=False,
)
cls.graded_score_weight_blocks[(graded, has_score, weight)] = block
host = os.environ.get('127.0.0.1')
host = '127.0.0.1'
metadata_lti_xblock = {
'lti_id': 'correct_lti_id',
'launch_url': 'http://{}:{}/{}'.format(host, '8765', 'correct_lti_endpoint'),

View File

@@ -13,26 +13,16 @@ class RemoteContextPlugin:
self.config = config
self.active = config.getoption("pytest-contexts")
def pytest_runtest_setup(self, item):
self.doit(item, "setup")
def pytest_runtest_teardown(self, item):
self.doit(item, "teardown")
def pytest_runtest_call(self, item):
self.doit(item, "call")
@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
config.pluginmanager.register(RemoteContextPlugin(config), "remotecontextplugin")
@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
config.pluginmanager.register(RemoteContextPlugin(config), "remotecontextplugin")
def pytest_addoption(parser):
group = parser.getgroup("coverage")
group.addoption(
"--pytest-remote-contexts",
action="store_true",
dest="pytest-contexts",
help="Capture the pytest contexts that coverage is being captured in in another process",
)
def pytest_addoption(parser):
group = parser.getgroup("coverage")
group.addoption(
"--pytest-remote-contexts",
action="store_true",
dest="pytest-contexts",
help="Capture the pytest contexts that coverage is being captured in in another process",
)

View File

@@ -122,29 +122,29 @@ class TestPaverJavaScriptTestTasks(PaverTestCase):
'node --max_old_space_size=4096 node_modules/.bin/karma start {options}'
)
# for suite in suites:
# # Karma test command
# if suite != 'jest-snapshot':
# karma_config_file = Env.KARMA_CONFIG_FILES[Env.JS_TEST_ID_KEYS.index(suite)]
# expected_test_tool_command = command_template.format(
# options=self.EXPECTED_KARMA_OPTIONS.format(
# config_file=karma_config_file,
# single_run='false' if dev_mode else 'true',
# suite=suite,
# platform_root=self.platform_root,
# browser=Env.KARMA_BROWSER,
# ),
# )
# if is_coverage:
# expected_test_tool_command += self.EXPECTED_COVERAGE_OPTIONS.format(
# platform_root=self.platform_root,
# suite=suite
# )
# if port:
# expected_test_tool_command += f" --port={port}"
# else:
# expected_test_tool_command = 'jest'
for suite in suites:
# Karma test command
if suite != 'jest-snapshot':
karma_config_file = Env.KARMA_CONFIG_FILES[Env.JS_TEST_ID_KEYS.index(suite)]
expected_test_tool_command = command_template.format(
options=self.EXPECTED_KARMA_OPTIONS.format(
config_file=karma_config_file,
single_run='false' if dev_mode else 'true',
suite=suite,
platform_root=self.platform_root,
browser=Env.KARMA_BROWSER,
),
)
if is_coverage:
expected_test_tool_command += self.EXPECTED_COVERAGE_OPTIONS.format(
platform_root=self.platform_root,
suite=suite
)
if port:
expected_test_tool_command += f" --port={port}"
else:
expected_test_tool_command = 'jest'
# expected_messages.append(expected_test_tool_command)
expected_messages.append(expected_test_tool_command)
assert self.task_messages == expected_messages

View File

@@ -77,34 +77,33 @@ class Env:
PRINT_SETTINGS_LOG_FILE = GEN_LOG_DIR / "print_settings.log"
# Detect if in a Docker container, and if so which one
SERVER_HOST = os.environ.get('0.0.0.0')
USING_DOCKER = SERVER_HOST != '0.0.0.0'
DEVSTACK_SETTINGS = 'devstack_docker' if USING_DOCKER else 'devstack'
# USING_DOCKER = SERVER_HOST != '0.0.0.0'
DEVSTACK_SETTINGS = 'devstack'
TEST_SETTINGS = 'test'
# Test Ids Directory
TEST_DIR = REPO_ROOT / ".testids"
# Configured browser to use for the js test suites
# SELENIUM_BROWSER = os.environ.get('SELENIUM_BROWSER', 'firefox')
SELENIUM_BROWSER = os.environ.get('SELENIUM_BROWSER', 'firefox')
# if USING_DOCKER:
# KARMA_BROWSER = 'ChromeDocker' if SELENIUM_BROWSER == 'chrome' else 'FirefoxDocker'
# else:
# KARMA_BROWSER = 'FirefoxNoUpdates'
KARMA_BROWSER = 'FirefoxNoUpdates'
# Files used to run each of the js test suites
# TODO: Store this as a dict. Order seems to matter for some
# reason. See issue TE-415.
# KARMA_CONFIG_FILES = [
# REPO_ROOT / 'cms/static/karma_cms.conf.js',
# REPO_ROOT / 'cms/static/karma_cms_squire.conf.js',
# REPO_ROOT / 'cms/static/karma_cms_webpack.conf.js',
# REPO_ROOT / 'lms/static/karma_lms.conf.js',
# REPO_ROOT / 'xmodule/js/karma_xmodule.conf.js',
# REPO_ROOT / 'xmodule/js/karma_xmodule_webpack.conf.js',
# REPO_ROOT / 'common/static/karma_common.conf.js',
# REPO_ROOT / 'common/static/karma_common_requirejs.conf.js',
# ]
KARMA_CONFIG_FILES = [
REPO_ROOT / 'cms/static/karma_cms.conf.js',
REPO_ROOT / 'cms/static/karma_cms_squire.conf.js',
REPO_ROOT / 'cms/static/karma_cms_webpack.conf.js',
REPO_ROOT / 'lms/static/karma_lms.conf.js',
REPO_ROOT / 'xmodule/js/karma_xmodule.conf.js',
REPO_ROOT / 'xmodule/js/karma_xmodule_webpack.conf.js',
REPO_ROOT / 'common/static/karma_common.conf.js',
REPO_ROOT / 'common/static/karma_common_requirejs.conf.js',
]
JS_TEST_ID_KEYS = [
'cms',

View File

@@ -40,9 +40,9 @@ passenv =
NPM_CONFIG_PREFIX
PYTHON_VERSION
SCRAPY_SETTINGS_MODULE
#SELENIUM_BROWSER
#SELENIUM_HOST
#SELENIUM_PORT
SELENIUM_BROWSER
SELENIUM_HOST
SELENIUM_PORT
SHARD
SKIP_NPM_INSTALL
SSH_AUTH_SOCK