Skip to content

Commit

Permalink
fix: json watcher parent problem (#1135)
Browse files Browse the repository at this point in the history
* fix: json watcher parent problem

Signed-off-by: axel7083 <[email protected]>

* Apply suggestions from code review

Signed-off-by: axel7083 <[email protected]>

* fix: prettier

Signed-off-by: axel7083 <[email protected]>

---------

Signed-off-by: axel7083 <[email protected]>
  • Loading branch information
axel7083 authored May 30, 2024
1 parent 4441e14 commit 05b29c6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/backend/src/utils/JsonWatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { promises, existsSync } from 'node:fs';
import { promises, existsSync, mkdirSync } from 'node:fs';
import type { FileSystemWatcher } from '@podman-desktop/api';
import { EventEmitter, fs } from '@podman-desktop/api';
import { JsonWatcher } from './JsonWatcher';
Expand All @@ -37,6 +37,7 @@ vi.mock('@podman-desktop/api', () => {
vi.mock('node:fs', () => {
return {
existsSync: vi.fn(),
mkdirSync: vi.fn(),
promises: {
readFile: vi.fn(),
},
Expand Down Expand Up @@ -68,6 +69,7 @@ test('should provide default value', async () => {
await vi.waitFor(() => {
expect(listener).toHaveBeenCalledWith('dummyDefaultvalue');
});
expect(mkdirSync).toHaveBeenCalled();
expect(existsSync).toHaveBeenCalledWith('dummyPath');
expect(promises.readFile).not.toHaveBeenCalled();
});
Expand Down
8 changes: 7 additions & 1 deletion packages/backend/src/utils/JsonWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import { type Disposable, type FileSystemWatcher, fs, EventEmitter, type Event } from '@podman-desktop/api';
import { promises, existsSync } from 'node:fs';
import { promises, existsSync, mkdirSync } from 'node:fs';
import path from 'node:path';

export class JsonWatcher<T> implements Disposable {
#fileSystemWatcher: FileSystemWatcher | undefined;
Expand All @@ -31,6 +32,11 @@ export class JsonWatcher<T> implements Disposable {

init(): void {
try {
// we create the parent directory of the watched content
// if the parent directory does not exists, the watcher is not initialized properly
mkdirSync(path.dirname(this.path), { recursive: true });

// create file system watcher
this.#fileSystemWatcher = fs.createFileSystemWatcher(this.path);
// Setup listeners
this.#fileSystemWatcher.onDidChange(this.onDidChange.bind(this));
Expand Down

0 comments on commit 05b29c6

Please sign in to comment.