<commit_before>/* @flow */

import fs from 'fs';
import path from 'path';
import zlib from 'zlib';

let lumo = require('../lumo');

jest.mock('jszip');
// this needs to be here or tests start randomly failing with `can't find .babelrc`
require('jszip');

describe('lumo', () => {
  const readFileSync = fs.readFileSync;
  const existsSync = fs.existsSync;

  beforeEach(() => {
    fs.readFileSync = jest.fn((p: string) => {
      if (/foo/.test(p)) {
        return 'fooContents';
      }
      throw new Error(`file doesn't exist: ${p}`);
    });
  });

  afterEach(() => {
    fs.readFileSync = readFileSync;
    fs.existsSync = existsSync;
  });

  describe('load', () => {
    describe('in __DEV__', () => {
      fs.existsSync = jest.fn(() => true);
      it('returns the contents of a (bundled) file when it exists', () => {
        expect(lumo.load('foo')).toBe('fooContents');
      });

      it("returns null when a file doesn't exist", () => {
        expect(lumo.load('nonExistent')).toBe(null);
      });
    });

    describe('in production', () => {
      const inflateSync = zlib.inflateSync;

      beforeEach(() => {
        jest.resetModules();
        zlib.inflateSync = jest.fn((x: string) => x);
        global.lumo = {
          internal: {
            embedded: {
              keys: jest.fn(() => ['foo']),
              get: jest.fn((resource: string) => {
                if (resource === 'foo') {
                  return 'fooContents';
                }
                return null;
              }),
            },
          },
        };

        __DEV__ = false;
        lumo = require('../lumo'); // eslint-disable-line global-require
      });

      afterEach(() => {
        __DEV__ = true;
        zlib.inflateSync = inflateSync;
        delete global.lumo;
      });

      it('returns the contents of a (bundled) file when it exists', () => {
        expect(lumo.load('foo')).toBe('fooContents');
      });

      it("returns null when a file doesn't exist", () => {
        expect(lumo.load('nonExistent')).toBe(null);
      });
    });
  });

  describe('readCache', () => {
    const statSync = fs.statSync;
    beforeEach(() => {
      fs.statSync = jest.fn((filename: string) => ({
        mtimeMs: new Date().getTime(),
      }));
    });

    afterEach(() => {
      fs.statSync = statSync;
    });

    it('returns the contents of a (cached) file when it exists', () => {
      expect(lumo.readCache('foo')).toEqual({
        source: 'fooContents',
        modified: expect.any(Number),
      });
    });

    it("returns null when a file doesn't exist", () => {
      expect(lumo.readCache('nonExistent')).toBe(null);
    });
  });

  describe('writeCache', () => {
    const writeFileSync = fs.writeFileSync;
    beforeEach(() => {
      fs.writeFileSync = jest.fn(
        (fname: string, contents: string, encoding: string) => {
          if (/foo/.test(fname)) {
            return;
          }
          throw new Error('some error');
        },
      );
    });

    afterEach(() => {
      fs.writeFileSync = writeFileSync;
    });

    it('writes correctly if directory exists', () => {
      expect(lumo.writeCache('foo', 'bar')).toBeUndefined();
    });

    it("catches and returns an error if it can't write", () => {
      expect(lumo.writeCache('nonExistent', 'contents')).toBeInstanceOf(Error);
    });
  });

  describe('readSource', () => {
    const pathResolve = path.resolve;

    beforeEach(() => {
      jest.resetModules();
      lumo = require('../lumo'); // eslint-disable-line global-require
      path.resolve = jest.fn((x: string) => x);
    });

    afterEach(() => {
      path.resolve = pathResolve;
    });

    it('cycles through the source paths', () => {
      const srcPaths = ['a', 'b', 'c'];
      lumo.addSourcePaths(srcPaths);
      const lumoPaths = [process.cwd(), ...srcPaths];

      fs.readFileSync = jest.fn((filename: string) => {
        throw new Error(`file doesn't exist: ${filename}`);
      });

      const source = lumo.readSource('bar/baz');
      const mockCalls = fs.readFileSync.mock.calls;

      expect(source).toBe(null);
      expect(
        mockCalls
          .map((x: string[]) => x[0])
          .filter(x =>
            new Set(lumoPaths.map((p: string) => path.join(p, 'bar/baz'))).has(
              x,
            ),
          ),
      ).toEqual(lumoPaths.map((p: string) => path.join(p, 'bar/baz')));
    });

    describe('reads JAR archives', () => {
      it('should return the source when JAR has the source', () => {
        const srcPaths = ['foo.jar'];
        lumo.addSourcePaths(srcPaths);

        const source = lumo.readSource('some/thing');

        expect(source).toEqual({
          source: 'zipContents',
          modified: expect.any(Number),
        });
      });

      it("should return null when the JAR doesn't have the source", () => {
        const source = lumo.readSource('some/thing');

        expect(source).toBe(null);
      });
    });
  });

  describe('loadUpstreamJsLibs', () => {
    beforeEach(() => {
      jest.resetModules();
      lumo = require('../lumo'); // eslint-disable-line global-require
    });

    it('should return an array with the file contents when JAR has deps.cljs', () => {
      const srcPaths = ['foo.jar'];
      lumo.addSourcePaths(srcPaths);

      const source = lumo.loadUpstreamJsLibs('some/thing');

      expect(source).toEqual(['zipContents']);
    });

    it("should return an empty array when the JAR doesn't have deps.cljs", () => {
      const source = lumo.loadUpstreamJsLibs('some/thing');

      expect(source).toEqual([]);
    });

    it("shouldn't crash when a JAR isn't found", () => {
      const srcPaths = ['bar.jar'];
      lumo.addSourcePaths(srcPaths);

      const source = lumo.loadUpstreamJsLibs('some/thing');

      expect(source).toEqual([]);
    });
  });

  describe('resource', () => {
    const pathResolve = path.resolve;

    beforeEach(() => {
      jest.resetModules();
      lumo = require('../lumo'); // eslint-disable-line global-require
      path.resolve = jest.fn((x: string) => x);
    });

    afterEach(() => {
      path.resolve = pathResolve;
    });

    it('cycles through the source paths', () => {
      const srcPaths = ['a', 'b', 'c'];
      lumo.addSourcePaths(srcPaths);
      const lumoPaths = [process.cwd(), ...srcPaths];

      fs.existsSync = jest.fn((_: string) => false);

      const exists = lumo.resource('bar/baz');
      const mockCalls = fs.existsSync.mock.calls;

      expect(exists).toBe(null);
      expect(fs.existsSync).toHaveBeenCalledTimes(5);
      expect(mockCalls.map((x: string[]) => x[0])).toEqual(
        ['./target/bar/baz'].concat(
          lumoPaths.map((p: string) => path.join(p, 'bar/baz')),
        ),
      );
    });

    it('returns the representation for the resource when it exists', () => {
      fs.existsSync = jest.fn((_: string) => true);
      expect(lumo.resource('some-file')).toEqual({
        type: 'bundled',
        src: 'some-file',
      });
    });

    describe('reads JAR archives', () => {
      it('should return true when JAR has the file', () => {
        const srcPaths = ['foo.jar'];
        lumo.addSourcePaths(srcPaths);

        fs.existsSync = jest.fn((fname: string) => /foo/.test(fname));

        expect(lumo.resource('some/thing')).toMatchObject({
          type: 'jar',
          src: 'some/thing',
        });
      });

      it("should return false when the JAR doesn't have the file", () => {
        expect(lumo.resource('some/thing')).toBe(null);
      });
    });
  });
});
<commit_msg>fix lint<commit_after>/* @flow */

import fs from 'fs';
import path from 'path';
import zlib from 'zlib';

let lumo = require('../lumo');

jest.mock('jszip');
// this needs to be here or tests start randomly failing with `can't find .babelrc`
require('jszip');

describe('lumo', () => {
  const readFileSync = fs.readFileSync;
  const existsSync = fs.existsSync;

  beforeEach(() => {
    fs.readFileSync = jest.fn((p: string) => {
      if (/foo/.test(p)) {
        return 'fooContents';
      }
      throw new Error(`file doesn't exist: ${p}`);
    });
  });

  afterEach(() => {
    fs.readFileSync = readFileSync;
    fs.existsSync = existsSync;
  });

  describe('load', () => {
    describe('in __DEV__', () => {
      fs.existsSync = jest.fn(() => true);
      it('returns the contents of a (bundled) file when it exists', () => {
        expect(lumo.load('foo')).toBe('fooContents');
      });

      it("returns null when a file doesn't exist", () => {
        expect(lumo.load('nonExistent')).toBe(null);
      });
    });

    describe('in production', () => {
      const inflateSync = zlib.inflateSync;

      beforeEach(() => {
        jest.resetModules();
        zlib.inflateSync = jest.fn((x: string) => x);
        global.lumo = {
          internal: {
            embedded: {
              keys: jest.fn(() => ['foo']),
              get: jest.fn((resource: string) => {
                if (resource === 'foo') {
                  return 'fooContents';
                }
                return null;
              }),
            },
          },
        };

        __DEV__ = false;
        lumo = require('../lumo'); // eslint-disable-line global-require
      });

      afterEach(() => {
        __DEV__ = true;
        zlib.inflateSync = inflateSync;
        delete global.lumo;
      });

      it('returns the contents of a (bundled) file when it exists', () => {
        expect(lumo.load('foo')).toBe('fooContents');
      });

      it("returns null when a file doesn't exist", () => {
        expect(lumo.load('nonExistent')).toBe(null);
      });
    });
  });

  describe('readCache', () => {
    const statSync = fs.statSync;
    beforeEach(() => {
      fs.statSync = jest.fn((filename: string) => ({
        mtimeMs: new Date().getTime(),
      }));
    });

    afterEach(() => {
      fs.statSync = statSync;
    });

    it('returns the contents of a (cached) file when it exists', () => {
      expect(lumo.readCache('foo')).toEqual({
        source: 'fooContents',
        modified: expect.any(Number),
      });
    });

    it("returns null when a file doesn't exist", () => {
      expect(lumo.readCache('nonExistent')).toBe(null);
    });
  });

  describe('writeCache', () => {
    const writeFileSync = fs.writeFileSync;
    beforeEach(() => {
      fs.writeFileSync = jest.fn(
        (fname: string, contents: string, encoding: string) => {
          if (/foo/.test(fname)) {
            return;
          }
          throw new Error('some error');
        },
      );
    });

    afterEach(() => {
      fs.writeFileSync = writeFileSync;
    });

    it('writes correctly if directory exists', () => {
      expect(lumo.writeCache('foo', 'bar')).toBeUndefined();
    });

    it("catches and returns an error if it can't write", () => {
      expect(lumo.writeCache('nonExistent', 'contents')).toBeInstanceOf(Error);
    });
  });

  describe('readSource', () => {
    const pathResolve = path.resolve;

    beforeEach(() => {
      jest.resetModules();
      lumo = require('../lumo'); // eslint-disable-line global-require
      path.resolve = jest.fn((x: string) => x);
    });

    afterEach(() => {
      path.resolve = pathResolve;
    });

    it('cycles through the source paths', () => {
      const srcPaths = ['a', 'b', 'c'];
      lumo.addSourcePaths(srcPaths);
      const lumoPaths = [process.cwd(), ...srcPaths];

      fs.readFileSync = jest.fn((filename: string) => {
        throw new Error(`file doesn't exist: ${filename}`);
      });

      const source = lumo.readSource('bar/baz');
      const mockCalls = fs.readFileSync.mock.calls;

      expect(source).toBe(null);
      expect(
        mockCalls
          .map((x: string[]) => x[0])
          .filter((x: string) =>
            new Set(lumoPaths.map((p: string) => path.join(p, 'bar/baz'))).has(
              x,
            ),
          ),
      ).toEqual(lumoPaths.map((p: string) => path.join(p, 'bar/baz')));
    });

    describe('reads JAR archives', () => {
      it('should return the source when JAR has the source', () => {
        const srcPaths = ['foo.jar'];
        lumo.addSourcePaths(srcPaths);

        const source = lumo.readSource('some/thing');

        expect(source).toEqual({
          source: 'zipContents',
          modified: expect.any(Number),
        });
      });

      it("should return null when the JAR doesn't have the source", () => {
        const source = lumo.readSource('some/thing');

        expect(source).toBe(null);
      });
    });
  });

  describe('loadUpstreamJsLibs', () => {
    beforeEach(() => {
      jest.resetModules();
      lumo = require('../lumo'); // eslint-disable-line global-require
    });

    it('should return an array with the file contents when JAR has deps.cljs', () => {
      const srcPaths = ['foo.jar'];
      lumo.addSourcePaths(srcPaths);

      const source = lumo.loadUpstreamJsLibs('some/thing');

      expect(source).toEqual(['zipContents']);
    });

    it("should return an empty array when the JAR doesn't have deps.cljs", () => {
      const source = lumo.loadUpstreamJsLibs('some/thing');

      expect(source).toEqual([]);
    });

    it("shouldn't crash when a JAR isn't found", () => {
      const srcPaths = ['bar.jar'];
      lumo.addSourcePaths(srcPaths);

      const source = lumo.loadUpstreamJsLibs('some/thing');

      expect(source).toEqual([]);
    });
  });

  describe('resource', () => {
    const pathResolve = path.resolve;

    beforeEach(() => {
      jest.resetModules();
      lumo = require('../lumo'); // eslint-disable-line global-require
      path.resolve = jest.fn((x: string) => x);
    });

    afterEach(() => {
      path.resolve = pathResolve;
    });

    it('cycles through the source paths', () => {
      const srcPaths = ['a', 'b', 'c'];
      lumo.addSourcePaths(srcPaths);
      const lumoPaths = [process.cwd(), ...srcPaths];

      fs.existsSync = jest.fn((_: string) => false);

      const exists = lumo.resource('bar/baz');
      const mockCalls = fs.existsSync.mock.calls;

      expect(exists).toBe(null);
      expect(fs.existsSync).toHaveBeenCalledTimes(5);
      expect(mockCalls.map((x: string[]) => x[0])).toEqual(
        ['./target/bar/baz'].concat(
          lumoPaths.map((p: string) => path.join(p, 'bar/baz')),
        ),
      );
    });

    it('returns the representation for the resource when it exists', () => {
      fs.existsSync = jest.fn((_: string) => true);
      expect(lumo.resource('some-file')).toEqual({
        type: 'bundled',
        src: 'some-file',
      });
    });

    describe('reads JAR archives', () => {
      it('should return true when JAR has the file', () => {
        const srcPaths = ['foo.jar'];
        lumo.addSourcePaths(srcPaths);

        fs.existsSync = jest.fn((fname: string) => /foo/.test(fname));

        expect(lumo.resource('some/thing')).toMatchObject({
          type: 'jar',
          src: 'some/thing',
        });
      });

      it("should return false when the JAR doesn't have the file", () => {
        expect(lumo.resource('some/thing')).toBe(null);
      });
    });
  });
});

{'fraction_non_alphanumeric': 0.18360375747224594, 'fraction_numerical': 0.0003941404453787033, 'mean_word_length': 2.255078041479581, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3195923', 'n_tokens_mistral': 4792, 'n_tokens_neox': 4707, 'n_words': 972}