Skip to content

Commit

Permalink
[test-visibility] Update test code owners extraction logic to use tes…
Browse files Browse the repository at this point in the history
…t source file (#4560)
  • Loading branch information
juan-fernandez authored Jul 30, 2024
1 parent f32deff commit ce92d59
Show file tree
Hide file tree
Showing 22 changed files with 364 additions and 17 deletions.
2 changes: 2 additions & 0 deletions integration-tests/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# CODEOWNERS for testing purposes
ci-visibility/subproject @datadog-dd-trace-js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"video": false,
"screenshotOnRunFailure": false,
"pluginsFile": "cypress/plugins-old/index.js",
"supportFile": "cypress/support/e2e.js",
"integrationFolder": "cypress/e2e",
"defaultCommandTimeout": 100,
"nodeVersion": "system"
}
11 changes: 11 additions & 0 deletions integration-tests/ci-visibility/subproject/cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
defaultCommandTimeout: 100,
e2e: {
setupNodeEvents (on, config) {
return require('dd-trace/ci/cypress/plugin')(on, config)
},
specPattern: process.env.SPEC_PATTERN || 'cypress/e2e/**/*.cy.js'
},
video: false,
screenshotOnRunFailure: false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable */
describe('context', () => {
it('passes', () => {
cy.visit('/')
.get('.hello-world')
.should('have.text', 'Hello World')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('dd-trace/ci/cypress/plugin')
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line
require('dd-trace/ci/cypress/support')
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Feature: Greetings
Scenario: Say greetings
When the greeter says greetings
Then I should have heard "greetings"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const assert = require('assert')
const { When, Then } = require('@cucumber/cucumber')
class Greeter {
sayGreetings () {
return 'greetings'
}
}

When('the greeter says greetings', function () {
this.whatIHeard = new Greeter().sayGreetings()
})

Then('I should have heard {string}', function (expectedResponse) {
assert.equal(this.whatIHeard, expectedResponse)
})
6 changes: 6 additions & 0 deletions integration-tests/ci-visibility/subproject/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "subproject",
"private": true,
"version": "1.0.0",
"description": "app within repo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { test, expect } = require('@playwright/test')

test.beforeEach(async ({ page }) => {
await page.goto(process.env.PW_BASE_URL)
})

test.describe('playwright', () => {
test('should work with passing tests', async ({ page }) => {
await expect(page.locator('.hello-world')).toHaveText([
'Hello World'
])
})
})
18 changes: 18 additions & 0 deletions integration-tests/ci-visibility/subproject/playwright.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Playwright config file for integration tests
const { devices } = require('@playwright/test')

const config = {
baseURL: process.env.PW_BASE_URL,
testDir: './playwright-tests',
timeout: 30000,
reporter: 'line',
projects: [
{
name: 'chromium',
use: devices['Desktop Chrome']
}
],
testMatch: '**/*-test.js'
}

module.exports = config
9 changes: 9 additions & 0 deletions integration-tests/ci-visibility/subproject/subproject-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// eslint-disable-next-line
const { expect } = require('chai')

describe('subproject-test', () => {
it('can run', () => {
// eslint-disable-next-line
expect(1).to.equal(1)
})
})
7 changes: 7 additions & 0 deletions integration-tests/ci-visibility/subproject/vitest-test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { describe, test, expect } from 'vitest'

describe('context', () => {
test('can report passed test', () => {
expect(1 + 2).to.equal(3)
})
})
2 changes: 1 addition & 1 deletion integration-tests/config-jest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
projects: [__dirname],
projects: process.env.PROJECTS ? JSON.parse(process.env.PROJECTS) : [__dirname],
testPathIgnorePatterns: ['/node_modules/'],
cache: false,
testMatch: [
Expand Down
33 changes: 32 additions & 1 deletion integration-tests/cucumber/cucumber.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ const {
TEST_IS_NEW,
TEST_IS_RETRY,
TEST_NAME,
CUCUMBER_IS_PARALLEL
CUCUMBER_IS_PARALLEL,
TEST_SUITE,
TEST_CODE_OWNERS
} = require('../../packages/dd-trace/src/plugins/util/test')

const isOldNode = semver.satisfies(process.version, '<=16')
Expand Down Expand Up @@ -1087,6 +1089,35 @@ versions.forEach(version => {
}
})
})

it('correctly calculates test code owners when working directory is not repository root', (done) => {
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)

const test = events.find(event => event.type === 'test').content
// The test is in a subproject
assert.notEqual(test.meta[TEST_SOURCE_FILE], test.meta[TEST_SUITE])
assert.equal(test.meta[TEST_CODE_OWNERS], JSON.stringify(['@datadog-dd-trace-js']))
})

childProcess = exec(
'node ../../node_modules/.bin/cucumber-js features/*.feature',
{
cwd: `${cwd}/ci-visibility/subproject`,
env: {
...getCiVisAgentlessConfig(receiver.port)
},
stdio: 'inherit'
}
)

childProcess.on('exit', () => {
eventsPromise.then(() => {
done()
}).catch(done)
})
})
})
})
})
50 changes: 49 additions & 1 deletion integration-tests/cypress/cypress.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const {
TEST_SOURCE_FILE,
TEST_IS_NEW,
TEST_IS_RETRY,
TEST_EARLY_FLAKE_ENABLED
TEST_EARLY_FLAKE_ENABLED,
TEST_SUITE,
TEST_CODE_OWNERS
} = require('../../packages/dd-trace/src/plugins/util/test')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')
const { NODE_MAJOR } = require('../../version')
Expand Down Expand Up @@ -1271,5 +1273,51 @@ moduleTypes.forEach(({
})
})
})

it('correctly calculates test code owners when working directory is not repository root', (done) => {
let command

if (type === 'commonJS') {
const commandSuffix = version === '6.7.0'
? '--config-file cypress-config.json --spec "cypress/e2e/*.cy.js"'
: ''
command = `../../node_modules/.bin/cypress run ${commandSuffix}`
} else {
command = `node --loader=${hookFile} ../../cypress-esm-config.mjs`
}

const {
NODE_OPTIONS, // NODE_OPTIONS dd-trace config does not work with cypress
...restEnvVars
} = getCiVisAgentlessConfig(receiver.port)

const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)

const test = events.find(event => event.type === 'test').content
// The test is in a subproject
assert.notEqual(test.meta[TEST_SOURCE_FILE], test.meta[TEST_SUITE])
assert.equal(test.meta[TEST_CODE_OWNERS], JSON.stringify(['@datadog-dd-trace-js']))
}, 25000)

childProcess = exec(
command,
{
cwd: `${cwd}/ci-visibility/subproject`,
env: {
...restEnvVars,
CYPRESS_BASE_URL: `http://localhost:${webAppPort}`
},
stdio: 'inherit'
}
)

childProcess.on('exit', () => {
eventsPromise.then(() => {
done()
}).catch(done)
})
})
})
})
35 changes: 34 additions & 1 deletion integration-tests/jest/jest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const {
TEST_NAME,
JEST_DISPLAY_NAME,
TEST_EARLY_FLAKE_ABORT_REASON,
TEST_SOURCE_START
TEST_SOURCE_START,
TEST_CODE_OWNERS
} = require('../../packages/dd-trace/src/plugins/util/test')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')

Expand Down Expand Up @@ -240,6 +241,38 @@ describe('jest CommonJS', () => {
})
})

it('correctly calculates test code owners when working directory is not repository root', (done) => {
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)

const test = events.find(event => event.type === 'test').content
// The test is in a subproject
assert.notEqual(test.meta[TEST_SOURCE_FILE], test.meta[TEST_SUITE])
assert.equal(test.meta[TEST_CODE_OWNERS], JSON.stringify(['@datadog-dd-trace-js']))
})

childProcess = exec(
'node ./node_modules/jest/bin/jest --config config-jest.js --rootDir ci-visibility/subproject',
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
PROJECTS: JSON.stringify([{
testMatch: ['**/subproject-test*']
}])
},
stdio: 'inherit'
}
)

childProcess.on('exit', () => {
eventsPromise.then(() => {
done()
}).catch(done)
})
})

it('works when sharding', (done) => {
receiver.payloadReceived(({ url }) => url === '/api/v2/citestcycle').then(events => {
const testSuiteEvents = events.payload.events.filter(event => event.type === 'test_suite_end')
Expand Down
32 changes: 31 additions & 1 deletion integration-tests/mocha/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const {
TEST_COMMAND,
TEST_MODULE,
MOCHA_IS_PARALLEL,
TEST_SOURCE_START
TEST_SOURCE_START,
TEST_CODE_OWNERS
} = require('../../packages/dd-trace/src/plugins/util/test')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')

Expand Down Expand Up @@ -240,6 +241,35 @@ describe('mocha CommonJS', function () {
})
})

it('correctly calculates test code owners when working directory is not repository root', (done) => {
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)

const test = events.find(event => event.type === 'test').content
// The test is in a subproject
assert.notEqual(test.meta[TEST_SOURCE_FILE], test.meta[TEST_SUITE])
assert.equal(test.meta[TEST_CODE_OWNERS], JSON.stringify(['@datadog-dd-trace-js']))
})

childProcess = exec(
'node ../../node_modules/mocha/bin/mocha subproject-test.js',
{
cwd: `${cwd}/ci-visibility/subproject`,
env: {
...getCiVisAgentlessConfig(receiver.port)
},
stdio: 'inherit'
}
)

childProcess.on('exit', () => {
eventsPromise.then(() => {
done()
}).catch(done)
})
})

it('does not change mocha config if CI Visibility fails to init', (done) => {
receiver.assertPayloadReceived(() => {
const error = new Error('it should not report tests')
Expand Down
36 changes: 35 additions & 1 deletion integration-tests/playwright/playwright.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const {
TEST_CONFIGURATION_BROWSER_NAME,
TEST_IS_NEW,
TEST_IS_RETRY,
TEST_EARLY_FLAKE_ENABLED
TEST_EARLY_FLAKE_ENABLED,
TEST_SUITE,
TEST_CODE_OWNERS
} = require('../../packages/dd-trace/src/plugins/util/test')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')

Expand Down Expand Up @@ -570,5 +572,37 @@ versions.forEach((version) => {
.catch(done)
})
})

it('correctly calculates test code owners when working directory is not repository root', (done) => {
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)

const test = events.find(event => event.type === 'test').content
// The test is in a subproject
assert.notEqual(test.meta[TEST_SOURCE_FILE], test.meta[TEST_SUITE])
assert.equal(test.meta[TEST_CODE_OWNERS], JSON.stringify(['@datadog-dd-trace-js']))
})

childProcess = exec(
'../../node_modules/.bin/playwright test',
{
cwd: `${cwd}/ci-visibility/subproject`,
env: {
...getCiVisAgentlessConfig(receiver.port),
PW_BASE_URL: `http://localhost:${webAppPort}`,
PW_RUNNER_DEBUG: '1',
TEST_DIR: '.'
},
stdio: 'inherit'
}
)

childProcess.on('exit', () => {
eventsPromise.then(() => {
done()
}).catch(done)
})
})
})
})
Loading

0 comments on commit ce92d59

Please sign in to comment.