kopia lustrzana https://github.com/Aircoookie/WLED
Automatically test cdata.js
rodzic
f3ae041691
commit
b4857ab036
|
@ -94,3 +94,16 @@ jobs:
|
||||||
*.bin
|
*.bin
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
|
||||||
|
Test-cdata:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Use Node.js
|
||||||
|
uses: actions/setup-node@v3
|
||||||
|
with:
|
||||||
|
node-version: '20.x'
|
||||||
|
cache: 'npm'
|
||||||
|
- run: npm ci
|
||||||
|
- run: npm test
|
|
@ -9,6 +9,7 @@
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "node tools/cdata.js",
|
"build": "node tools/cdata.js",
|
||||||
|
"test": "node --test",
|
||||||
"dev": "nodemon -e js,html,htm,css,png,jpg,gif,ico,js -w tools/ -w wled00/data/ -x node tools/cdata.js"
|
"dev": "nodemon -e js,html,htm,css,png,jpg,gif,ico,js -w tools/ -w wled00/data/ -x node tools/cdata.js"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|
|
@ -1,75 +1,100 @@
|
||||||
|
'use strict';
|
||||||
const assert = require('node:assert');
|
const assert = require('node:assert');
|
||||||
const { describe, it, before, after, mock, test } = require('node:test');
|
const { describe, it, before, after, afterEach, mock, test, run } = require('node:test');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const child_process = require('child_process');
|
||||||
|
const util = require('util');
|
||||||
|
const execPromise = util.promisify(child_process.exec);
|
||||||
|
|
||||||
|
process.env.NODE_ENV = 'test'; // Set the environment to testing
|
||||||
const cdata = require('./cdata.js');
|
const cdata = require('./cdata.js');
|
||||||
|
|
||||||
describe('Caching', function () {
|
describe('Caching', () => {
|
||||||
describe('isFileNewerThan', function () {
|
const testFolderPath = path.join(__dirname, 'testFolder');
|
||||||
before(function () {
|
const oldFilePath = path.join(testFolderPath, 'oldFile.txt');
|
||||||
// Create a temporary file before the test
|
const newFilePath = path.join(testFolderPath, 'newFile.txt');
|
||||||
fs.writeFileSync('temp.txt', 'Hello World');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return true if the file is newer than the provided time', function () {
|
// Create a temporary file before the test
|
||||||
|
before(() => {
|
||||||
|
fs.writeFileSync('temp.txt', 'Hello World');
|
||||||
|
|
||||||
|
// Create test folder
|
||||||
|
if (!fs.existsSync(testFolderPath)) {
|
||||||
|
fs.mkdirSync(testFolderPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create an old file
|
||||||
|
fs.writeFileSync(oldFilePath, 'This is an old file.');
|
||||||
|
// Modify the 'mtime' to simulate an old file
|
||||||
|
const oldTime = new Date();
|
||||||
|
oldTime.setFullYear(oldTime.getFullYear() - 1);
|
||||||
|
fs.utimesSync(oldFilePath, oldTime, oldTime);
|
||||||
|
|
||||||
|
// Create a new file
|
||||||
|
fs.writeFileSync(newFilePath, 'This is a new file.');
|
||||||
|
});
|
||||||
|
// delete the temporary file after the test
|
||||||
|
after(() => {
|
||||||
|
fs.unlinkSync('temp.txt');
|
||||||
|
|
||||||
|
// Delete test folder
|
||||||
|
if (fs.existsSync(testFolderPath)) {
|
||||||
|
fs.rmSync(testFolderPath, { recursive: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('isFileNewerThan', async () => {
|
||||||
|
it('should return true if the file is newer than the provided time', async () => {
|
||||||
const pastTime = Date.now() - 10000; // 10 seconds ago
|
const pastTime = Date.now() - 10000; // 10 seconds ago
|
||||||
assert.equal(cdata.isFileNewerThan('temp.txt', pastTime), true);
|
assert.strictEqual(cdata.isFileNewerThan('temp.txt', pastTime), true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false if the file is older than the provided time', function () {
|
it('should return false if the file is older than the provided time', async () => {
|
||||||
const futureTime = Date.now() + 10000; // 10 seconds in the future
|
const futureTime = Date.now() + 10000; // 10 seconds in the future
|
||||||
assert.equal(cdata.isFileNewerThan('temp.txt', futureTime), false);
|
assert.strictEqual(cdata.isFileNewerThan('temp.txt', futureTime), false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false if the file does not exist', function () {
|
it('should return false if the file does not exist', async () => {
|
||||||
assert.equal(cdata.isFileNewerThan('nonexistent.txt', Date.now()), false);
|
assert.strictEqual(cdata.isFileNewerThan('nonexistent.txt', Date.now()), false);
|
||||||
});
|
|
||||||
|
|
||||||
// delete the temporary file after the test
|
|
||||||
after(function () {
|
|
||||||
fs.unlinkSync('temp.txt');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('isAnyFileInFolderNewerThan', function () {
|
describe('isAnyFileInFolderNewerThan', async () => {
|
||||||
const testFolderPath = path.join(__dirname, 'testFolder');
|
it('should return true if a file in the folder is newer than the given time', async () => {
|
||||||
const oldFilePath = path.join(testFolderPath, 'oldFile.txt');
|
|
||||||
const newFilePath = path.join(testFolderPath, 'newFile.txt');
|
|
||||||
|
|
||||||
before(function () {
|
|
||||||
// Create test folder
|
|
||||||
if (!fs.existsSync(testFolderPath)) {
|
|
||||||
fs.mkdirSync(testFolderPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create an old file
|
|
||||||
fs.writeFileSync(oldFilePath, 'This is an old file.');
|
|
||||||
// Modify the 'mtime' to simulate an old file
|
|
||||||
const oldTime = new Date();
|
|
||||||
oldTime.setFullYear(oldTime.getFullYear() - 1);
|
|
||||||
fs.utimesSync(oldFilePath, oldTime, oldTime);
|
|
||||||
|
|
||||||
// Create a new file
|
|
||||||
fs.writeFileSync(newFilePath, 'This is a new file.');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return true if a file in the folder is newer than the given time', function () {
|
|
||||||
const folderPath = path.join(__dirname, 'testFolder');
|
const folderPath = path.join(__dirname, 'testFolder');
|
||||||
const time = fs.statSync(path.join(folderPath, 'oldFile.txt')).mtime;
|
const time = fs.statSync(path.join(folderPath, 'oldFile.txt')).mtime;
|
||||||
assert.strictEqual(cdata.isAnyFileInFolderNewerThan(folderPath, time), true);
|
assert.strictEqual(cdata.isAnyFileInFolderNewerThan(folderPath, time), true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false if no files in the folder are newer than the given time', function () {
|
it('should return false if no files in the folder are newer than the given time', async () => {
|
||||||
const folderPath = path.join(__dirname, 'testFolder');
|
const folderPath = path.join(__dirname, 'testFolder');
|
||||||
const time = new Date();
|
const time = new Date();
|
||||||
assert.strictEqual(cdata.isAnyFileInFolderNewerThan(folderPath, time), false);
|
assert.strictEqual(cdata.isAnyFileInFolderNewerThan(folderPath, time), false);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
after(function () {
|
describe('General functionality', () => {
|
||||||
// Delete test folder
|
describe('Script', () => {
|
||||||
if (fs.existsSync(testFolderPath)) {
|
it('should create html_*.h files if they are missing', async () => {
|
||||||
fs.rmSync(testFolderPath, { recursive: true });
|
// delete all html_*.h files
|
||||||
}
|
const folderPath = 'wled00';
|
||||||
|
let files = await fs.promises.readdir(folderPath);
|
||||||
|
await Promise.all(files.map(file => {
|
||||||
|
if (file.startsWith('html_') && path.extname(file) === '.h') {
|
||||||
|
return fs.promises.unlink(path.join(folderPath, file));
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
// run script cdata.js and wait for it to finish
|
||||||
|
process.env.NODE_ENV = 'production';
|
||||||
|
await execPromise('node tools/cdata.js');
|
||||||
|
|
||||||
|
// check if html_*.h files were created
|
||||||
|
files = await fs.promises.readdir(folderPath);
|
||||||
|
const htmlFiles = files.filter(file => file.startsWith('html_') && path.extname(file) === '.h');
|
||||||
|
assert(htmlFiles.length > 0, 'html_*.h files were not created');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
|
@ -233,6 +233,7 @@ function isAnyFileInFolderNewerThan(folderPath, time) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the web UI is already built
|
||||||
function isAlreadyBuilt(folderPath) {
|
function isAlreadyBuilt(folderPath) {
|
||||||
let lastBuildTime = Infinity;
|
let lastBuildTime = Infinity;
|
||||||
|
|
||||||
|
@ -248,6 +249,11 @@ function isAlreadyBuilt(folderPath) {
|
||||||
return !isAnyFileInFolderNewerThan(folderPath, lastBuildTime);
|
return !isAnyFileInFolderNewerThan(folderPath, lastBuildTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't run this script if we're in a test environment
|
||||||
|
if (process.env.NODE_ENV === 'test') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (isAlreadyBuilt("wled00/data") && process.argv[2] !== '--force' && process.argv[2] !== '-f') {
|
if (isAlreadyBuilt("wled00/data") && process.argv[2] !== '--force' && process.argv[2] !== '-f') {
|
||||||
console.info("Web UI is already built");
|
console.info("Web UI is already built");
|
||||||
return;
|
return;
|
||||||
|
|
Ładowanie…
Reference in New Issue