kopia lustrzana https://github.com/Aircoookie/WLED
Automatically test cdata.js
rodzic
f3ae041691
commit
b4857ab036
|
@ -94,3 +94,16 @@ jobs:
|
|||
*.bin
|
||||
env:
|
||||
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": {
|
||||
"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"
|
||||
},
|
||||
"repository": {
|
||||
|
|
|
@ -1,42 +1,24 @@
|
|||
'use strict';
|
||||
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 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');
|
||||
|
||||
describe('Caching', function () {
|
||||
describe('isFileNewerThan', function () {
|
||||
before(function () {
|
||||
// Create a temporary file before the test
|
||||
fs.writeFileSync('temp.txt', 'Hello World');
|
||||
});
|
||||
|
||||
it('should return true if the file is newer than the provided time', function () {
|
||||
const pastTime = Date.now() - 10000; // 10 seconds ago
|
||||
assert.equal(cdata.isFileNewerThan('temp.txt', pastTime), true);
|
||||
});
|
||||
|
||||
it('should return false if the file is older than the provided time', function () {
|
||||
const futureTime = Date.now() + 10000; // 10 seconds in the future
|
||||
assert.equal(cdata.isFileNewerThan('temp.txt', futureTime), false);
|
||||
});
|
||||
|
||||
it('should return false if the file does not exist', function () {
|
||||
assert.equal(cdata.isFileNewerThan('nonexistent.txt', Date.now()), false);
|
||||
});
|
||||
|
||||
// delete the temporary file after the test
|
||||
after(function () {
|
||||
fs.unlinkSync('temp.txt');
|
||||
});
|
||||
});
|
||||
|
||||
describe('isAnyFileInFolderNewerThan', function () {
|
||||
describe('Caching', () => {
|
||||
const testFolderPath = path.join(__dirname, 'testFolder');
|
||||
const oldFilePath = path.join(testFolderPath, 'oldFile.txt');
|
||||
const newFilePath = path.join(testFolderPath, 'newFile.txt');
|
||||
|
||||
before(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);
|
||||
|
@ -52,24 +34,67 @@ describe('Caching', function () {
|
|||
// Create a new file
|
||||
fs.writeFileSync(newFilePath, 'This is a new file.');
|
||||
});
|
||||
// delete the temporary file after the test
|
||||
after(() => {
|
||||
fs.unlinkSync('temp.txt');
|
||||
|
||||
it('should return true if a file in the folder is newer than the given time', function () {
|
||||
const folderPath = path.join(__dirname, 'testFolder');
|
||||
const time = fs.statSync(path.join(folderPath, 'oldFile.txt')).mtime;
|
||||
assert.strictEqual(cdata.isAnyFileInFolderNewerThan(folderPath, time), true);
|
||||
});
|
||||
|
||||
it('should return false if no files in the folder are newer than the given time', function () {
|
||||
const folderPath = path.join(__dirname, 'testFolder');
|
||||
const time = new Date();
|
||||
assert.strictEqual(cdata.isAnyFileInFolderNewerThan(folderPath, time), false);
|
||||
});
|
||||
|
||||
after(function () {
|
||||
// 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
|
||||
assert.strictEqual(cdata.isFileNewerThan('temp.txt', pastTime), true);
|
||||
});
|
||||
|
||||
it('should return false if the file is older than the provided time', async () => {
|
||||
const futureTime = Date.now() + 10000; // 10 seconds in the future
|
||||
assert.strictEqual(cdata.isFileNewerThan('temp.txt', futureTime), false);
|
||||
});
|
||||
|
||||
it('should return false if the file does not exist', async () => {
|
||||
assert.strictEqual(cdata.isFileNewerThan('nonexistent.txt', Date.now()), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isAnyFileInFolderNewerThan', async () => {
|
||||
it('should return true if a file in the folder is newer than the given time', async () => {
|
||||
const folderPath = path.join(__dirname, 'testFolder');
|
||||
const time = fs.statSync(path.join(folderPath, 'oldFile.txt')).mtime;
|
||||
assert.strictEqual(cdata.isAnyFileInFolderNewerThan(folderPath, time), true);
|
||||
});
|
||||
|
||||
it('should return false if no files in the folder are newer than the given time', async () => {
|
||||
const folderPath = path.join(__dirname, 'testFolder');
|
||||
const time = new Date();
|
||||
assert.strictEqual(cdata.isAnyFileInFolderNewerThan(folderPath, time), false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('General functionality', () => {
|
||||
describe('Script', () => {
|
||||
it('should create html_*.h files if they are missing', async () => {
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Check if the web UI is already built
|
||||
function isAlreadyBuilt(folderPath) {
|
||||
let lastBuildTime = Infinity;
|
||||
|
||||
|
@ -248,6 +249,11 @@ function isAlreadyBuilt(folderPath) {
|
|||
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') {
|
||||
console.info("Web UI is already built");
|
||||
return;
|
||||
|
|
Ładowanie…
Reference in New Issue