Skip to content

refactor: Handle no credentials condidtion #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 74 additions & 64 deletions src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
mergeVideos?: boolean;
}

export interface Credentials {
username: string;
accessKey: string;
}

// Types of attachments relevant for UI display.
const webAssetsTypes = [
'.log',
Expand All @@ -52,8 +57,19 @@
'.svg',
];

const hasCredentials = function () {
return process.env.SAUCE_USERNAME && process.env.SAUCE_ACCESS_KEY;
};

const getCredentials = function (): Credentials {
return {
username: process.env.SAUCE_USERNAME,
accessKey: process.env.SAUCE_ACCESS_KEY,
} as Credentials;
};

export default class SauceReporter implements Reporter {
projects: { [k: string]: any };

Check warning on line 72 in src/reporter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

buildName: string;
tags: string[];
Expand Down Expand Up @@ -93,21 +109,25 @@

constructor(reporterConfig: Config) {
this.projects = {};

this.buildName = reporterConfig?.buildName || '';
this.tags = reporterConfig?.tags || [];
this.region = reporterConfig?.region || 'us-west-1';
this.outputFile =
reporterConfig?.outputFile || process.env.SAUCE_REPORT_OUTPUT_NAME;
this.shouldUpload = reporterConfig?.upload !== false;
this.mergeVideos = reporterConfig?.mergeVideos === true;
this.playwrightVersion = 'unknown';

this.webAssetsDir =
reporterConfig.webAssetsDir || process.env.SAUCE_WEB_ASSETS_DIR;
if (this.webAssetsDir && !fs.existsSync(this.webAssetsDir)) {
fs.mkdirSync(this.webAssetsDir, { recursive: true });
}

if (!hasCredentials() || !this.shouldUpload) {
return;
}

let reporterVersion = 'unknown';
try {
const packageData = JSON.parse(
Expand All @@ -118,28 +138,20 @@
/* empty */
}

if (
process.env.SAUCE_USERNAME &&
process.env.SAUCE_USERNAME !== '' &&
process.env.SAUCE_ACCESS_KEY &&
process.env.SAUCE_ACCESS_KEY !== ''
) {
this.api = new TestComposer({
region: this.region,
username: process.env.SAUCE_USERNAME,
accessKey: process.env.SAUCE_ACCESS_KEY,
headers: {
'User-Agent': `playwright-reporter/${reporterVersion}`,
},
});
this.testRunsApi = new TestRunsApi({
region: this.region,
username: process.env.SAUCE_USERNAME,
accessKey: process.env.SAUCE_ACCESS_KEY,
});
}

this.playwrightVersion = 'unknown';
const { username, accessKey } = getCredentials();
this.api = new TestComposer({
region: this.region,
username,
accessKey,
headers: {
'User-Agent': `playwright-reporter/${reporterVersion}`,
},
});
this.testRunsApi = new TestRunsApi({
region: this.region,
username,
accessKey,
});
}

onBegin(config: FullConfig, suite: PlaywrightSuite) {
Expand Down Expand Up @@ -168,24 +180,22 @@
for await (const projectSuite of this.rootSuite.suites) {
const { report, assets } = await this.createSauceReport(projectSuite);

const result = await this.reportToSauce(projectSuite, report, assets);
suites.push(...report.suites);

if (this.isWebAssetSyncEnabled()) {
this.syncAssets(assets);
}

if (!hasCredentials() || !this.shouldUpload) {
continue;
}

const result = await this.reportToSauce(projectSuite, report, assets);
if (result?.id) {
jobUrls.push({
url: result.url,
name: projectSuite.title,
});
try {
await this.reportTestRun(projectSuite, report, result?.id);
} catch (e: any) {
console.warn('failed to send report to insights: ', e);
}
}

suites.push(...report.suites);

if (this.isWebAssetSyncEnabled()) {
this.syncAssets(assets);
}
}

Expand Down Expand Up @@ -233,7 +243,11 @@
};
}

await this.testRunsApi?.create([req]);
try {
await this.testRunsApi?.create([req]);
} catch (e: any) {

Check warning on line 248 in src/reporter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
console.warn('failed to send report to insights: ', e);
}
}

getDuration(projectSuite: PlaywrightSuite) {
Expand Down Expand Up @@ -280,17 +294,14 @@
}

displayReportedJobs(jobs: { name: string; url: string }[]) {
if (!hasCredentials() && this.shouldUpload) {
console.warn(
`\nNo results reported to Sauce Labs. SAUCE_USERNAME and SAUCE_ACCESS_KEY environment variables must be defined in order for reports to be uploaded to Sauce.`,
);
console.log();
return;
}
if (jobs.length < 1) {
let msg = '';
const hasCredentials =
process.env.SAUCE_USERNAME &&
process.env.SAUCE_USERNAME !== '' &&
process.env.SAUCE_ACCESS_KEY &&
process.env.SAUCE_ACCESS_KEY !== '';
if (hasCredentials && this.shouldUpload) {
msg = `\nNo results reported to Sauce. $SAUCE_USERNAME and $SAUCE_ACCESS_KEY environment variables must be defined in order for reports to be uploaded to Sauce.`;
}
console.log(msg);
console.log();
return;
}
Expand Down Expand Up @@ -529,25 +540,24 @@
const browserVersion = '1.0';
const browserName = this.getBrowserName(projectSuite);

if (this.shouldUpload) {
const resp = await this.api?.createReport({
name: projectSuite.title,
browserName: `playwright-${browserName}`,
browserVersion,
platformName: this.getPlatformName(),
framework: 'playwright',
frameworkVersion: this.playwrightVersion,
passed: didSuitePass,
startTime: this.startedAt?.toISOString() ?? new Date().toISOString(),
endTime: this.endedAt?.toISOString() ?? new Date().toISOString(),
build: this.buildName,
tags: this.tags,
});
if (resp?.id) {
await this.uploadAssets(resp.id, consoleLog, report, assets);
}
return resp;
const resp = await this.api?.createReport({
name: projectSuite.title,
browserName: `playwright-${browserName}`,
browserVersion,
platformName: this.getPlatformName(),
framework: 'playwright',
frameworkVersion: this.playwrightVersion,
passed: didSuitePass,
startTime: this.startedAt?.toISOString() ?? new Date().toISOString(),
endTime: this.endedAt?.toISOString() ?? new Date().toISOString(),
build: this.buildName,
tags: this.tags,
});
if (resp?.id) {
await this.uploadAssets(resp.id, consoleLog, report, assets);
await this.reportTestRun(projectSuite, report, resp.id);
}
return resp;
}

async uploadAssets(
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/playwright.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('runs tests on cloud', function () {
beforeAll(async function () {
if (!process.env.SAUCE_USERNAME || !process.env.SAUCE_ACCESS_KEY) {
throw new Error(
'Please set SAUCE_USERNAME and SAUCE_ACCESS_KEY env variables',
'Please set SAUCE_USERNAME and SAUCE_ACCESS_KEY environment variables',
);
}

Expand Down
Loading