Skip to content

Commit 5b65f43

Browse files
author
CI Fix
committed
test/ migrated to esm in test-esm/
1 parent 0357fea commit 5b65f43

File tree

206 files changed

+17541
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

206 files changed

+17541
-0
lines changed

test-esm/convert-tests.mjs

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#!/usr/bin/env node
2+
3+
import fs from 'fs-extra'
4+
import path from 'path'
5+
import { fileURLToPath } from 'url'
6+
import globPkg from 'glob'
7+
const { glob } = globPkg
8+
9+
const __filename = fileURLToPath(import.meta.url)
10+
const __dirname = path.dirname(__filename)
11+
12+
const projectRoot = path.resolve(__dirname, '..')
13+
const originalTestDir = path.join(projectRoot, 'test')
14+
const esmTestDir = path.join(projectRoot, 'test-esm')
15+
16+
// Conversion patterns for CommonJS to ESM
17+
const conversionPatterns = [
18+
// Basic require statements
19+
{
20+
pattern: /const\s+(\w+)\s*=\s*require\((['"`])(.*?)\2\)/g,
21+
replacement: "import $1 from '$3'"
22+
},
23+
{
24+
pattern: /const\s*\{\s*([^}]+)\s*\}\s*=\s*require\((['"`])(.*?)\2\)/g,
25+
replacement: "import { $1 } from '$3'"
26+
},
27+
// module.exports to export
28+
{
29+
pattern: /module\.exports\s*=\s*/g,
30+
replacement: 'export default '
31+
},
32+
{
33+
pattern: /exports\.(\w+)\s*=\s*/g,
34+
replacement: 'export const $1 = '
35+
},
36+
// Add use strict removal
37+
{
38+
pattern: /['"]use strict['"];\s*\n?/g,
39+
replacement: ''
40+
},
41+
// Update relative require paths to .mjs
42+
{
43+
pattern: /(import.*from\s+['"`])(\.\.?\/[^'"`]*?)(['"`])/g,
44+
replacement: (match, prefix, path, suffix) => {
45+
if (!path.includes('.')) {
46+
return match // Keep as is if no extension
47+
}
48+
const newPath = path.replace(/\.js$/, '.mjs')
49+
return prefix + newPath + suffix
50+
}
51+
}
52+
]
53+
54+
function convertFileContent(content, fileName) {
55+
let converted = content
56+
57+
// Apply conversion patterns
58+
conversionPatterns.forEach(({ pattern, replacement }) => {
59+
if (typeof replacement === 'function') {
60+
converted = converted.replace(pattern, replacement)
61+
} else {
62+
converted = converted.replace(pattern, replacement)
63+
}
64+
})
65+
66+
// Add ESM specific imports at the top
67+
const esmImports = [
68+
"import { describe, it, beforeEach, afterEach, before, after } from 'mocha'",
69+
"import { fileURLToPath } from 'url'",
70+
"import path from 'path'",
71+
"import { createRequire } from 'module'",
72+
"",
73+
"const require = createRequire(import.meta.url)",
74+
"const __filename = fileURLToPath(import.meta.url)",
75+
"const __dirname = path.dirname(__filename)",
76+
""
77+
]
78+
79+
// Only add if not already present
80+
if (!converted.includes('import.meta.url')) {
81+
converted = esmImports.join('\n') + '\n' + converted
82+
}
83+
84+
return converted
85+
}
86+
87+
async function convertTestFile(sourceFile, targetFile) {
88+
try {
89+
const content = await fs.readFile(sourceFile, 'utf8')
90+
const convertedContent = convertFileContent(content, path.basename(sourceFile))
91+
92+
// Ensure target directory exists
93+
await fs.ensureDir(path.dirname(targetFile))
94+
95+
// Write converted file
96+
await fs.writeFile(targetFile, convertedContent, 'utf8')
97+
98+
console.log(`✓ Converted: ${path.relative(projectRoot, sourceFile)}${path.relative(projectRoot, targetFile)}`)
99+
100+
return true
101+
} catch (error) {
102+
console.error(`✗ Error converting ${sourceFile}:`, error.message)
103+
return false
104+
}
105+
}
106+
107+
async function convertAllTests() {
108+
console.log('Converting CommonJS tests to ESM...\n')
109+
110+
// Find all .js test files
111+
const testFiles = await glob('**/*.js', { cwd: originalTestDir, nodir: true })
112+
113+
let successCount = 0
114+
let failCount = 0
115+
116+
for (const testFile of testFiles) {
117+
const sourceFile = path.join(originalTestDir, testFile)
118+
const targetFile = path.join(esmTestDir, testFile.replace(/\.js$/, '.mjs'))
119+
120+
const success = await convertTestFile(sourceFile, targetFile)
121+
if (success) {
122+
successCount++
123+
} else {
124+
failCount++
125+
}
126+
}
127+
128+
console.log(`\nConversion complete!`)
129+
console.log(`✓ Successful: ${successCount}`)
130+
console.log(`✗ Failed: ${failCount}`)
131+
132+
if (failCount > 0) {
133+
console.log('\nNote: Some files may require manual review and adjustment.')
134+
}
135+
136+
return { successCount, failCount }
137+
}
138+
139+
// Run if called directly
140+
if (process.argv[1] === __filename) {
141+
convertAllTests()
142+
.then(({ successCount, failCount }) => {
143+
process.exit(failCount > 0 ? 1 : 0)
144+
})
145+
.catch(error => {
146+
console.error('Conversion failed:', error)
147+
process.exit(1)
148+
})
149+
}
150+
151+
export default convertAllTests
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// This test file is currently commented out in the original CommonJS version
2+
// Converting to ESM for completeness
3+
4+
// const supertest = require('supertest')
5+
// // Helper functions for the FS
6+
// const $rdf = require('rdflib')
7+
//
8+
// const { rm, read } = require('../utils')
9+
// const ldnode = require('../../index')
10+
// const fs = require('fs-extra')
11+
// const path = require('path')
12+
//
13+
// describe('AccountManager (TLS account creation tests)', function () {
14+
// var address = 'https://localhost:3457'
15+
// var host = 'localhost:3457'
16+
// var ldpHttpsServer
17+
// let rootPath = path.join(__dirname, '../resources/accounts/')
18+
// var ldp = ldnode.createServer({
19+
// root: rootPath,
20+
// sslKey: path.join(__dirname, '../keys/key.pem'),
21+
// sslCert: path.join(__dirname, '../keys/cert.pem'),
22+
// auth: 'tls',
23+
// webid: true,
24+
// multiuser: true,
25+
// strictOrigin: true
26+
// })
27+
//
28+
// before(function (done) {
29+
// ldpHttpsServer = ldp.listen(3457, done)
30+
// })
31+
//
32+
// after(function () {
33+
// if (ldpHttpsServer) ldpHttpsServer.close()
34+
// })
35+
//
36+
// describe('Account creation', function () {
37+
// it('should create an account directory', function (done) {
38+
// var subdomain = supertest('https://nicola.' + host)
39+
// subdomain.post('/')
40+
// .send(spkacPost)
41+
// .expect(200)
42+
// .end(function (err, res) {
43+
// var subdomain = supertest('https://nicola.' + host)
44+
// subdomain.head('/')
45+
// .expect(401)
46+
// .end(function (err) {
47+
// done(err)
48+
// })
49+
// })
50+
// })
51+
//
52+
// it('should create a profile for the user', function (done) {
53+
// var subdomain = supertest('https://nicola.' + host)
54+
// subdomain.head('/profile/card')
55+
// .expect(401)
56+
// .end(function (err) {
57+
// done(err)
58+
// })
59+
// })
60+
//
61+
// it('should create a preferences file in the account directory', function (done) {
62+
// var subdomain = supertest('https://nicola.' + host)
63+
// subdomain.head('/prefs.ttl')
64+
// .expect(401)
65+
// .end(function (err) {
66+
// done(err)
67+
// })
68+
// })
69+
//
70+
// it('should create a workspace container', function (done) {
71+
// var subdomain = supertest('https://nicola.' + host)
72+
// subdomain.head('/Public/')
73+
// .expect(401)
74+
// .end(function (err) {
75+
// done(err)
76+
// })
77+
// })
78+
//
79+
// it('should create a private profile file in the settings container', function (done) {
80+
// var subdomain = supertest('https://nicola.' + host)
81+
// subdomain.head('/settings/serverSide.ttl')
82+
// .expect(401)
83+
// .end(function (err) {
84+
// done(err)
85+
// })
86+
// })
87+
//
88+
// it('should create a private prefs file in the settings container', function (done) {
89+
// var subdomain = supertest('https://nicola.' + host)
90+
// subdomain.head('/inbox/prefs.ttl')
91+
// .expect(401)
92+
// .end(function (err) {
93+
// done(err)
94+
// })
95+
// })
96+
//
97+
// it('should create a private inbox container', function (done) {
98+
// var subdomain = supertest('https://nicola.' + host)
99+
// subdomain.head('/inbox/')
100+
// .expect(401)
101+
// .end(function (err) {
102+
// done(err)
103+
// })
104+
// })
105+
// })
106+
// })
107+
108+
// ESM equivalent (all commented out as in original)
109+
// import supertest from 'supertest'
110+
// import $rdf from 'rdflib'
111+
// import { rm, read } from '../../test/utils.js'
112+
// import ldnode from '../../index.js'
113+
// import fs from 'fs-extra'
114+
// import path from 'path'
115+
// import { fileURLToPath } from 'url'
116+
//
117+
// const __filename = fileURLToPath(import.meta.url)
118+
// const __dirname = path.dirname(__filename)
119+
120+
// Since the entire test is commented out, this ESM file contains no active tests
121+
// This preserves the original behavior while providing ESM format for consistency
122+
123+
describe('AccountManager (TLS account creation tests) - ESM placeholder', function () {
124+
it('should be a placeholder test (original file is commented out)', function () {
125+
// This test passes to maintain consistency with the commented-out original
126+
})
127+
})

0 commit comments

Comments
 (0)