Loading files
82965f623This file is pinned to the selected release. Download the original to open it in your design tool.
import assert from 'node:assert/strict'
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { test } from 'node:test'
import { prepareCoreS3IdfDependencies } from './idf-dependencies.mjs'
test('prepares both managed components without duplicating them', () => {
const outputDirectory = mkdtempSync(path.join(tmpdir(), 'stackchan-idf-dependencies-'))
try {
const manifestPath = prepareCoreS3IdfDependencies({
outputDirectory,
platformName: 'm5stackchan_cores3',
applicationName: 'stack-chan-host',
mode: 'debug',
})
const first = readFileSync(manifestPath, 'utf8')
prepareCoreS3IdfDependencies({
outputDirectory,
platformName: 'm5stackchan_cores3',
applicationName: 'stack-chan-host',
mode: 'debug',
})
const second = readFileSync(manifestPath, 'utf8')
assert.equal(second, first)
assert.equal(count(second, 'espressif/esp-sr:'), 1)
assert.equal(count(second, 'espressif/esp_audio_codec:'), 1)
assert.equal(count(second, 'espressif/esp32-camera:'), 1)
} finally {
rmSync(outputDirectory, { recursive: true, force: true })
}
})
test('prepares the built-in CoreS3 camera dependency', () => {
const outputDirectory = mkdtempSync(path.join(tmpdir(), 'stackchan-idf-dependencies-'))
try {
const manifestPath = prepareCoreS3IdfDependencies({
outputDirectory,
platformName: 'm5stack_cores3',
applicationName: 'stack-chan-host',
mode: 'release',
})
const manifest = readFileSync(manifestPath, 'utf8')
assert.equal(count(manifest, 'espressif/esp-sr:'), 1)
assert.equal(count(manifest, 'espressif/esp32-camera:'), 1)
assert.equal(count(manifest, 'espressif/esp_audio_codec:'), 0)
} finally {
rmSync(outputDirectory, { recursive: true, force: true })
}
})
test('uses the generated directory for each ESP32 build mode', () => {
const outputDirectory = mkdtempSync(path.join(tmpdir(), 'stackchan-idf-dependencies-'))
try {
for (const mode of ['debug', 'instrument', 'release']) {
const manifestPath = prepareCoreS3IdfDependencies({
outputDirectory,
platformName: 'm5stackchan_cores3',
applicationName: 'stack-chan-host',
mode,
})
assert.equal(
manifestPath,
path.join(
outputDirectory,
'tmp',
'esp32',
'm5stackchan_cores3',
mode,
'stack-chan-host',
'xsProj-esp32s3',
'main',
'idf_component.yml',
),
)
}
} finally {
rmSync(outputDirectory, { recursive: true, force: true })
}
})
test('invalidates a configured project when its dependency lock is stale', () => {
const outputDirectory = mkdtempSync(path.join(tmpdir(), 'stackchan-idf-dependencies-'))
const projectDirectory = path.join(outputDirectory, 'tmp/esp32/m5stack_cores3/debug/stack-chan-host/xsProj-esp32s3')
const sdkconfigHeaderPath = path.join(projectDirectory, 'build/config/sdkconfig.h')
try {
mkdirSync(path.dirname(sdkconfigHeaderPath), { recursive: true })
writeFileSync(path.join(projectDirectory, 'dependencies.lock'), 'dependencies:\n espressif/esp32-camera:\n')
writeFileSync(sdkconfigHeaderPath, '#define CONFIG_IDF_TARGET_ESP32S3 1\n')
prepareCoreS3IdfDependencies({
outputDirectory,
platformName: 'm5stack_cores3',
applicationName: 'stack-chan-host',
mode: 'debug',
})
assert.equal(existsSync(sdkconfigHeaderPath), false)
} finally {
rmSync(outputDirectory, { recursive: true, force: true })
}
})
test('invalidates a configured project when an exact dependency version is obsolete', () => {
const outputDirectory = mkdtempSync(path.join(tmpdir(), 'stackchan-idf-dependencies-'))
const projectDirectory = path.join(outputDirectory, 'tmp/esp32/m5stack_cores3/debug/stack-chan-host/xsProj-esp32s3')
const sdkconfigHeaderPath = path.join(projectDirectory, 'build/config/sdkconfig.h')
try {
mkdirSync(path.dirname(sdkconfigHeaderPath), { recursive: true })
writeFileSync(
path.join(projectDirectory, 'dependencies.lock'),
dependencyLock([
['espressif/esp-sr', '2.4.5'],
['espressif/esp32-camera', '2.1.7'],
]),
)
writeFileSync(sdkconfigHeaderPath, '#define CONFIG_IDF_TARGET_ESP32S3 1\n')
prepareCoreS3IdfDependencies({
outputDirectory,
platformName: 'm5stack_cores3',
applicationName: 'stack-chan-host',
mode: 'debug',
})
assert.equal(existsSync(sdkconfigHeaderPath), false)
} finally {
rmSync(outputDirectory, { recursive: true, force: true })
}
})
test('refreshes existing managed constraints before invalidating an obsolete lock', () => {
const outputDirectory = mkdtempSync(path.join(tmpdir(), 'stackchan-idf-dependencies-'))
const projectDirectory = path.join(
outputDirectory,
'tmp/esp32/m5stackchan_cores3/debug/stack-chan-host/xsProj-esp32s3',
)
const manifestPath = path.join(projectDirectory, 'main/idf_component.yml')
const sdkconfigHeaderPath = path.join(projectDirectory, 'build/config/sdkconfig.h')
try {
mkdirSync(path.dirname(manifestPath), { recursive: true })
mkdirSync(path.dirname(sdkconfigHeaderPath), { recursive: true })
writeFileSync(
manifestPath,
"dependencies:\n idf:\n version: '>=4.1.0'\n espressif/esp-sr: 2.4.5\n espressif/esp_audio_codec:\n version: ^2.5.0\n espressif/esp32-camera: ^2.0.10\n",
)
writeFileSync(
path.join(projectDirectory, 'dependencies.lock'),
dependencyLock([
['espressif/esp-sr', '2.4.5'],
['espressif/esp_audio_codec', '2.6.1'],
['espressif/esp32-camera', '2.1.7'],
]),
)
writeFileSync(sdkconfigHeaderPath, '#define CONFIG_IDF_TARGET_ESP32S3 1\n')
prepareCoreS3IdfDependencies({
outputDirectory,
platformName: 'm5stackchan_cores3',
applicationName: 'stack-chan-host',
mode: 'debug',
})
const manifest = readFileSync(manifestPath, 'utf8')
assert.match(manifest, /^ {2}espressif\/esp-sr: 2\.4\.6$/m)
assert.doesNotMatch(manifest, /^ {2}espressif\/esp-sr: 2\.4\.5$/m)
assert.match(manifest, /^ {4}version: \^2\.6\.0$/m)
assert.doesNotMatch(manifest, /^ {4}version: \^2\.5\.0$/m)
assert.equal(existsSync(sdkconfigHeaderPath), false)
} finally {
rmSync(outputDirectory, { recursive: true, force: true })
}
})
test('keeps configured projects when exact and caret dependency versions remain compatible', () => {
const outputDirectory = mkdtempSync(path.join(tmpdir(), 'stackchan-idf-dependencies-'))
const projectDirectory = path.join(outputDirectory, 'tmp/esp32/m5stack_cores3/debug/stack-chan-host/xsProj-esp32s3')
const sdkconfigHeaderPath = path.join(projectDirectory, 'build/config/sdkconfig.h')
try {
mkdirSync(path.dirname(sdkconfigHeaderPath), { recursive: true })
writeFileSync(
path.join(projectDirectory, 'dependencies.lock'),
dependencyLock([
['espressif/esp-sr', '2.4.6'],
['espressif/esp32-camera', '2.1.7'],
]),
)
writeFileSync(sdkconfigHeaderPath, '#define CONFIG_IDF_TARGET_ESP32S3 1\n')
prepareCoreS3IdfDependencies({
outputDirectory,
platformName: 'm5stack_cores3',
applicationName: 'stack-chan-host',
mode: 'debug',
})
assert.equal(existsSync(sdkconfigHeaderPath), true)
} finally {
rmSync(outputDirectory, { recursive: true, force: true })
}
})
/**
* Counts non-overlapping occurrences in a string.
* @param {string} source - Text to search.
* @param {string} value - Value to count.
* @returns {number} Number of occurrences.
*/
function count(source, value) {
return source.split(value).length - 1
}
function dependencyLock(entries) {
return `dependencies:\n${entries.map(([name, version]) => ` ${name}:\n version: ${version}\n`).join('')}`
}