summaryrefslogtreecommitdiffhomepage
path: root/gui/test/logging.spec.ts
blob: 22c3db02faff892c5e9d390e93f17d0b2ded700e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { expect } from 'chai';
import fs from 'fs';
import sinon from 'sinon';
import { it, describe, before, beforeEach, after } from 'mocha';
import { backupLogFile, rotateOrDeleteFile } from '../src/main/logging';

const aPath = 'log-directory/a.log';
const oldAPath = 'log-directory/a.old.log';
const bPath = 'log-directory/b.log';
const oldBPath = 'log-directory/b.old.log';

const initialFileState = {
  [aPath]: 'a',
  [bPath]: 'b',
  [oldBPath]: 'old b',
};

describe('Logging', () => {
  let files: Record<string, string>;
  let sandbox: sinon.SinonSandbox;

  before(() => {
    sandbox = sinon.createSandbox();

    sandbox.stub(fs, 'accessSync').callsFake((path) => {
      if (files[path as string] === undefined) {
        throw Error('File not found');
      }
    });

    sandbox.stub(fs, 'renameSync').callsFake((oldPath, newPath) => {
      files[newPath as string] = files[oldPath as string];
      fs.unlinkSync(oldPath);
    });

    sandbox.stub(fs, 'unlinkSync').callsFake((path) => {
      delete files[path as string];
    });

    sandbox.stub(fs, 'readFileSync').callsFake((path) => {
      return files[path as string] ?? '';
    });
  });

  after(() => {
    sandbox.restore();
  });

  beforeEach(() => {
    files = { ...initialFileState };
  });

  it('should backup log file', () => {
    backupLogFile(aPath);
    const oldA = fs.readFileSync(oldAPath).toString();

    expect(fs.accessSync.bind(null, aPath)).to.throw();
    expect(oldA).to.equal(initialFileState[aPath]);
  });

  it('should replace backup file', () => {
    backupLogFile(bPath);
    const oldB = fs.readFileSync(oldBPath).toString();

    expect(fs.accessSync.bind(null, bPath)).to.throw();
    expect(oldB).to.equal(initialFileState[bPath]);
  });

  it('should clean up old log files', () => {
    rotateOrDeleteFile(bPath);
    const oldB = fs.readFileSync(oldBPath).toString();

    expect(fs.accessSync.bind(null, bPath)).to.throw();
    expect(oldB).to.equal(initialFileState[bPath]);

    rotateOrDeleteFile(bPath);

    expect(fs.accessSync.bind(null, bPath)).to.throw();
    expect(fs.accessSync.bind(null, oldBPath)).to.throw();
  });
});