Skip to content

Commit

Permalink
chore: update utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie committed May 15, 2024
1 parent 79fb982 commit 57c3bc6
Show file tree
Hide file tree
Showing 24 changed files with 151 additions and 180 deletions.
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"trailingComma": "all",
"printWidth": 120
}
2 changes: 1 addition & 1 deletion test/cache-buster/cacheBuster.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { existsSync } from 'node:fs';
import { join } from 'upath';
import { describe, expect, it } from 'vitest';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../../shared/test/index.js';
import { cacheBuster } from '../../src/cache-buster/index.js';
import { Asset, AssetPack, path } from '../../src/core/index.js';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';

const pkg = 'cache-buster';

Expand Down
2 changes: 1 addition & 1 deletion test/core/AssetWatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, expect, it } from 'vitest';
import { assetPath, createFolder, getInputDir } from '../../shared/test/index.js';
import { Asset } from '../../src/core/Asset.js';
import { AssetCache } from '../../src/core/AssetCache.js';
import { AssetWatcher } from '../../src/core/AssetWatcher.js';
import { logAssetGraph } from '../../src/core/utils/logAssetGraph.js';
import { assetPath, createFolder, getInputDir } from '../utils/index.js';

const pkg = 'core';

Expand Down
78 changes: 74 additions & 4 deletions test/core/Assetpack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ import fs from 'fs-extra';
import { existsSync } from 'node:fs';
import { join } from 'path';
import { describe, expect, it } from 'vitest';
import { cacheBuster } from '../../src/cache-buster/cacheBuster.js';
import { AssetPack } from '../../src/core/AssetPack.js';
import { getHash } from '../../src/core/index.js';
import { logAssetGraph } from '../../src/core/utils/logAssetGraph.js';
import { pixiManifest } from '../../src/manifest/pixiManifest.js';
import {
assetPath,
createAssetPipe,
createFolder,
getCacheDir,
getInputDir,
getOutputDir
} from '../../shared/test/index.js';
import { AssetPack } from '../../src/core/AssetPack.js';
import { logAssetGraph } from '../../src/core/utils/logAssetGraph.js';
} from '../utils/index.js';

import type { MockAssetPipe } from '../../shared/test/index.js';
import type { AssetPipe } from '../../src/core/pipes/AssetPipe.js';
import type { MockAssetPipe } from '../utils/index.js';

const pkg = 'core';

Expand Down Expand Up @@ -125,6 +128,73 @@ describe('Core', () =>
expect(fs.readJSONSync(join(outputDir, 'json.json'))).toStrictEqual({ nice: 'test' });
});

it('should delete previously hashed versions of an asset', { timeout: 10000 }, async () =>
{
const testName = 'watch-delete-hash';
const inputDir = `${getInputDir(pkg, testName)}/`;
const outputDir = getOutputDir(pkg, testName);

createFolder(
pkg,
{
name: testName,
files: [{
name: 'json.json',
content: assetPath('json/json.json'),
}],
folders: [],
});

const testFile = join(inputDir, 'json.json');

const assetpack = new AssetPack({
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
output: outputDir,
cache: true,
pipes: [
cacheBuster(),
pixiManifest(),
]
});

await assetpack.watch();

const origHash = getHash(join(inputDir, 'json.json'));

expect(existsSync(join(outputDir, `json-${origHash}.json`))).toBe(true);

fs.writeJSONSync(testFile, { nice: 'test' });

await new Promise((resolve) =>
{
setTimeout(resolve, 1500);
});

expect(existsSync(join(outputDir, `json-${origHash}.json`))).toBe(false);

Check failure on line 173 in test/core/Assetpack.test.ts

View workflow job for this annotation

GitHub Actions / CI (Unit tests, test)

test/core/Assetpack.test.ts > Core > should delete previously hashed versions of an asset

AssertionError: expected true to be false // Object.is equality - Expected + Received - false + true ❯ test/core/Assetpack.test.ts:173:70
const newHash = getHash(join(inputDir, 'json.json'));

expect(existsSync(join(outputDir, `json-${newHash}.json`))).toBe(true);

fs.removeSync(testFile);

await new Promise((resolve) =>
{
setTimeout(resolve, 1500);
});

await assetpack.stop();

expect(existsSync(join(outputDir, `json-${origHash}.json`))).toBe(false);
expect(existsSync(join(outputDir, `json-${newHash}.json`))).toBe(false);
expect(fs.readJSONSync(join(outputDir, 'manifest.json'))).toStrictEqual({
bundles: [
{
name: 'default',
assets: []
}]
});
});

it('should ignore specified files when watching', async () =>
{
const testName = 'watch-ignore';
Expand Down
2 changes: 1 addition & 1 deletion test/core/Utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, it } from 'vitest';
import { createAssetPipe, createFolder, getCacheDir, getInputDir, getOutputDir } from '../../shared/test/index.js';
import { AssetPack } from '../../src/core/AssetPack.js';
import { extractTagsFromFileName } from '../../src/core/utils/extractTagsFromFileName.js';
import { generateCacheName } from '../../src/core/utils/generateCacheName.js';
import { createAssetPipe, createFolder, getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';

import type { Asset } from '../../src/core/Asset.js';
import type { AssetPipe } from '../../src/core/index.js';
Expand Down
2 changes: 1 addition & 1 deletion test/core/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { createAssetPipe } from '../../shared/test/index.js';
import { AssetPack } from '../../src/core/index.js';
import { createAssetPipe } from '../utils/index.js';

import type { AssetPackConfig } from '../../src/core/config.js';

Expand Down
2 changes: 1 addition & 1 deletion test/ffmpeg/Audio.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { existsSync } from 'node:fs';
import { describe, expect, it, vi } from 'vitest';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../../shared/test/index.js';
import { AssetPack } from '../../src/core/index.js';
import { audio } from '../../src/ffmpeg/index.js';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';

const pkg = 'ffmpeg';

Expand Down
2 changes: 1 addition & 1 deletion test/image/Compress.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { existsSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../../shared/test/index.js';
import { AssetPack } from '../../src/core/index.js';
import { compress } from '../../src/image/compress.js';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';

const pkg = 'image';

Expand Down
2 changes: 1 addition & 1 deletion test/image/Mipmap.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { existsSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../../shared/test/index.js';
import { AssetPack } from '../../src/core/index.js';
import { mipmap } from '../../src/image/mipmap.js';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';

const pkg = 'image';

Expand Down
2 changes: 1 addition & 1 deletion test/json/Json.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { existsSync, readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../../shared/test/index.js';
import { AssetPack } from '../../src/core/index.js';
import { json } from '../../src/json/index.js';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';

const pkg = 'json';

Expand Down
16 changes: 8 additions & 8 deletions test/manifest/Manifest.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import fs from 'fs-extra';
import { existsSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import {
assetPath,
createFolder,
getCacheDir,
getInputDir,
getOutputDir
} from '../../shared/test/index.js';
import { AssetPack } from '../../src/core/index.js';
import { audio } from '../../src/ffmpeg/index.js';
import { compress, mipmap } from '../../src/image/index.js';
import { pixiManifest } from '../../src/manifest/index.js';
import { spineAtlasManifestMod, spineAtlasMipmap } from '../../src/spine/index.js';
import { texturePacker, texturePackerManifestMod } from '../../src/texture-packer/index.js';
import {
assetPath,
createFolder,
getCacheDir,
getInputDir,
getOutputDir
} from '../utils/index.js';

import type { File } from '../../shared/test/index.js';
import type { File } from '../utils/index.js';

const pkg = 'manifest';

Expand Down
2 changes: 1 addition & 1 deletion test/spine/spineAtlasAll.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { glob } from 'glob';
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../../shared/test/index.js';
import { cacheBuster } from '../../src/cache-buster/index.js';
import { AssetPack } from '../../src/core/index.js';
import { compress, mipmap } from '../../src/image/index.js';
import { spineAtlasCacheBuster } from '../../src/spine/spineAtlasCacheBuster.js';
import { spineAtlasCompress } from '../../src/spine/spineAtlasCompress.js';
import { spineAtlasMipmap } from '../../src/spine/spineAtlasMipmap.js';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';

const pkg = 'spine';

Expand Down
2 changes: 1 addition & 1 deletion test/spine/spineAtlasCacheBuster.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { glob } from 'glob';
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../../shared/test/index.js';
import { cacheBuster } from '../../src/cache-buster/index.js';
import { AssetPack } from '../../src/core/index.js';
import { spineAtlasCacheBuster } from '../../src/spine/spineAtlasCacheBuster.js';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';

const pkg = 'spine';

Expand Down
2 changes: 1 addition & 1 deletion test/spine/spineAtlasCompress.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../../shared/test/index.js';
import { AssetPack } from '../../src/core/index.js';
import { compress } from '../../src/image/index.js';
import { spineAtlasCompress } from '../../src/spine/spineAtlasCompress.js';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';

const pkg = 'spine';

Expand Down
2 changes: 1 addition & 1 deletion test/spine/spineAtlasManifest.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../../shared/test/index.js';
import { AssetPack } from '../../src/core/index.js';
import { pixiManifest } from '../../src/manifest/index.js';
import { spineAtlasManifestMod } from '../../src/spine/spineAtlasManifestMod.js';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';

const pkg = 'spine';

Expand Down
2 changes: 1 addition & 1 deletion test/spine/spineAtlasMipmap.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { existsSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../../shared/test/index.js';
import { AssetPack } from '../../src/core/index.js';
import { mipmap } from '../../src/image/index.js';
import { spineAtlasMipmap } from '../../src/spine/spineAtlasMipmap.js';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';

const pkg = 'spine';

Expand Down
41 changes: 8 additions & 33 deletions test/texture-packer/texturePacker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,15 @@ import fs from 'fs-extra';
import { existsSync } from 'node:fs';
import sharp from 'sharp';
import { describe, expect, it, vi } from 'vitest';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../../shared/test/index.js';
import { AssetPack, Logger } from '../../src/core/index.js';
import { texturePacker } from '../../src/texture-packer/texturePacker.js';
import { createTPSFolder } from '../utils/createTPSFolder.js';
import { assetPath, createFolder, getCacheDir, getInputDir, getOutputDir } from '../utils/index.js';

import type { File } from '../../shared/test/index.js';
import type { File } from '../utils/index.js';

const pkg = 'texture-packer';

function genFolder(testName: string)
{
const sprites: File[] = [];

for (let i = 0; i < 10; i++)
{
sprites.push({
name: `sprite${i}.png`,
content: assetPath(`image/sp-${i + 1}.png`),
});
}
createFolder(
pkg,
{
name: testName,
files: [],
folders: [
{
name: 'sprites{tps}',
files: sprites,
folders: [],
},
],
});
}

describe('Texture Packer', () =>
{
it('should create a sprite sheet', async () =>
Expand All @@ -44,7 +19,7 @@ describe('Texture Packer', () =>
const inputDir = getInputDir(pkg, testName);
const outputDir = getOutputDir(pkg, testName);

genFolder(testName);
createTPSFolder(testName, pkg);

const assetpack = new AssetPack({
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
Expand Down Expand Up @@ -83,7 +58,7 @@ describe('Texture Packer', () =>
const inputDir = getInputDir(pkg, testName);
const outputDir = getOutputDir(pkg, testName);

genFolder(testName);
createTPSFolder(testName, pkg);

const size = 512;

Expand Down Expand Up @@ -119,7 +94,7 @@ describe('Texture Packer', () =>
const inputDir = getInputDir(pkg, testName);
const outputDir = getOutputDir(pkg, testName);

genFolder(testName);
createTPSFolder(testName, pkg);

const assetpack = new AssetPack({
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
Expand Down Expand Up @@ -152,7 +127,7 @@ describe('Texture Packer', () =>
const inputDir = getInputDir(pkg, testName);
const outputDir = getOutputDir(pkg, testName);

genFolder(testName);
createTPSFolder(testName, pkg);

const assetpack = new AssetPack({
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
Expand Down Expand Up @@ -276,7 +251,7 @@ describe('Texture Packer', () =>
const inputDir = getInputDir(pkg, testName);
const outputDir = getOutputDir(pkg, testName);

genFolder(testName);
createTPSFolder(testName, pkg);

const assetpack = new AssetPack({
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
Expand Down
Loading

0 comments on commit 57c3bc6

Please sign in to comment.