Skip to content

Commit

Permalink
Committing working test - second version. Expanded the grep to find h…
Browse files Browse the repository at this point in the history
…ttp and also nested elements
  • Loading branch information
antowaddle committed Dec 12, 2024
1 parent 2ce985b commit 65b62d4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import path from 'path';

describe('Verify that all the URLs referenced in the Manifest directory are operational', () => {
it('Reads the manifest directory, filters out unwanted URLs, and validates the remaining URLs', () => {
//const baseDir = process.env.BASE_DIR || '/Users/acoughli';
//const manifestsDir: string = path.resolve(baseDir, 'forked-odh-dashboard/odh-dashboard/manifests');
// Use `__dirname` to determine the path relative to the current test file
const manifestsDir = path.resolve(__dirname, '../../../../../odh-dashboard/manifests');
const baseDir = process.env.BASE_DIR || '/Users/acoughli';
const manifestsDir: string = path.resolve(
baseDir,
'forked-odh-dashboard/odh-dashboard/manifests',
);

cy.log('Resolved manifests directory:', manifestsDir);

Expand All @@ -15,11 +16,19 @@ describe('Verify that all the URLs referenced in the Manifest directory are oper
const filteredUrls = urls.filter(
(url) =>
!url.includes('my-project-s2i-python-service') &&
!url.includes('clusterip/') &&
!url.includes('ClusterIP') &&
!url.includes('s2i-python-service') &&
!url.includes('user-dev-rhoam-quarkus') &&
!url.includes('software.intel') &&
!url.includes('docs.openvino') &&
!url.includes('project-simple') &&
!url.includes('example.apps') &&
!url.includes('figma.com/figma/ns') &&
!url.includes('localhost') &&
!url.includes('red_hat_3scale_api_management') &&
!url.includes('anaconda.org/training/anaconda_introduction/notebook') &&
!url.includes('scikit-learn.org/stable/tutorial/index.html') &&
!url.includes('console-openshift-console.apps.test-cluster.example.com/') &&
!url.includes('console-openshift-console.apps.test-cluster.example.com') &&
!url.includes('ibm.com/docs/SSQNUZ_latest/svc-welcome/watsonxai.html') &&
Expand Down
90 changes: 60 additions & 30 deletions frontend/src/__tests__/cypress/cypress/utils/urlExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,90 @@ export function extractLauncherUrls(): Cypress.Chainable<string[]> {
return extractedUrls;
});
}

/**
* Extracts HTTPS URLs from YAML files in specified directories.
*
* @param {string} directory - The directory path to search for YAML files.
* @returns {string[]} An array of HTTPS URLs extracted from YAML files.
* Type definitions for YAML values.
*/
type YamlValue = string | number | boolean | null | YamlObject | YamlArray;
interface YamlObject {
[key: string]: YamlValue;
}
type YamlArray = YamlValue[];

export function extractHttpsUrls(directory: string): string[] {
const httpsUrlSet = new Set<string>();

function extractUrlsFromValue(value: YamlValue): void {
if (typeof value === 'string') {
const urlRegex = /https:\/\/[^\s\](),"'}*]*(?=[\s\](),"'}*]|$)/g;
const matches = value.match(urlRegex);
if (matches) {
matches.forEach((url) => {
const cleanUrl = url.replace(/\*+$/, '');
httpsUrlSet.add(cleanUrl);
});
}
} else if (typeof value === 'object' && value !== null) {
if (Array.isArray(value)) {
value.forEach(extractUrlsFromValue);
} else {
Object.values(value).forEach(extractUrlsFromValue);
}
/**
* Extracts URLs from a given value and adds them to the provided set.
*
* @param {YamlValue} value - The value to extract URLs from.
* @param {Set<string>} urlSet - A set to store unique URLs.
*/
function extractUrlsFromValue(value: YamlValue, urlSet: Set<string>): void {
if (typeof value === 'string') {
const urlRegex = /https?:\/\/[^\s\](),"'}*]*(?=[\s\](),"'}*]|$)/g; // Matches both HTTP and HTTPS
const matches = value.match(urlRegex);
if (matches) {
matches.forEach((url) => {
const cleanUrl = url.replace(/\*+$/, ''); // Remove trailing asterisks if any
urlSet.add(cleanUrl); // Add URL to the set for uniqueness
});
}
} else if (typeof value === 'object' && value !== null) {
if (Array.isArray(value)) {
value.forEach((item) => extractUrlsFromValue(item, urlSet)); // Process array items
} else {
Object.values(value).forEach((val) => extractUrlsFromValue(val, urlSet)); // Process object values
}
}
}

/**
* Extracts HTTPS URLs from YAML files in specified directories.
*
* @param {string} directory - The directory path to search for YAML files.
* @returns {string[]} An array of HTTPS URLs extracted from YAML files.
*/
export function extractHttpsUrls(directory: string): string[] {
const httpsUrlSet = new Set<string>();

function walkDir(dir: string): void {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
walkDir(filePath);
walkDir(filePath); // Recursively walk into subdirectories
} else if (file.endsWith('.yaml') || file.endsWith('.yml')) {
try {
const content = fs.readFileSync(filePath, 'utf8');
const yamlContent = yaml.load(content) as YamlValue;
extractUrlsFromValue(yamlContent);
const yamlDocuments = yaml.loadAll(content); // Load multiple YAML documents

// Process each document
yamlDocuments.forEach((yamlContent) => {
// Check if yamlContent is an object or array and extract URLs
if (typeof yamlContent === 'object' && yamlContent !== null) {
// Check for 'spec' key and extract URL if it exists
if (
'spec' in yamlContent &&
typeof yamlContent.spec === 'object' &&
yamlContent.spec !== null
) {
if ('url' in yamlContent.spec) {
// Use type assertion to specify that spec.url is a string or undefined
extractUrlsFromValue(yamlContent.spec.url as string, httpsUrlSet);
}
}

// Process all other values to find additional URLs
Object.values(yamlContent).forEach((value) =>
extractUrlsFromValue(value, httpsUrlSet),
);
}
});
} catch (error) {
// Catches any file related errors
// Catches file reading errors
}
}
});
}

walkDir(directory);
return Array.from(httpsUrlSet);
walkDir(directory); // Start walking the directory
return Array.from(httpsUrlSet); // Return the unique set of URLs as an array
}

0 comments on commit 65b62d4

Please sign in to comment.