Skip to content

Commit

Permalink
feat: add resolver log level (#242)
Browse files Browse the repository at this point in the history
Closes #239
  • Loading branch information
raae authored Aug 1, 2023
1 parent 27cf536 commit b8cf805
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 31 deletions.
41 changes: 41 additions & 0 deletions demo/src/pages/manual-tests/varid-data-no-logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import { graphql, useStaticQuery } from 'gatsby';
import { GatsbyImage, getImage } from 'gatsby-plugin-image';

const VariedDataPage = () => {
const data = useStaticQuery(graphql`
query {
allVariedData {
nodes {
name
expected
gatsbyImageData(
height: 200
backgroundColor: "#BADA55"
logLevel: "error"
)
}
}
}
`);

return data.allVariedData.nodes.map((node, index) => {
const gatsbyImage = getImage(node);

return (
<>
<h2>{node.name}</h2>
<div>
<strong>Expected:</strong> {node.expected}
</div>
{gatsbyImage ? (
<GatsbyImage key={index} image={gatsbyImage} alt={node.name} />
) : (
<div>No image for node with name: {node.name}</div>
)}
</>
);
});
};

export default VariedDataPage;
5 changes: 4 additions & 1 deletion plugin/gatsby-plugin-image/resolve-asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const {
} = require('./asset-data');
const { Joi } = require('gatsby-plugin-utils/joi');

const { resolverReporter } = require('./resolver-reporter');

const generateCloudinaryAssetSource = (
filename,
width,
Expand Down Expand Up @@ -94,7 +96,8 @@ exports._generateCloudinaryAssetSource = generateCloudinaryAssetSource;

exports.createResolveCloudinaryAssetData =
(gatsbyUtils) => async (source, args, _context, info) => {
const { reporter } = gatsbyUtils;
let { reporter } = gatsbyUtils;
reporter = resolverReporter({ reporter, logLevel: args.logLevel });
const transformType = info.parentType || 'UnknownTransformType';

const schema = Joi.object({
Expand Down
121 changes: 91 additions & 30 deletions plugin/gatsby-plugin-image/resolve-asset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,61 @@ describe('resolveCloudinaryAssetData', () => {
expect(result).toBe(null);
});

it('calls reporter.verbose and returns null for weird source', async () => {
const source = {
one: 'thing',
another: 'thang',
};
const args = {};
const result = await resolveCloudinaryAssetData(
source,
args,
context,
info
);
expect(generateImageData).toBeCalledTimes(0);
expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(1);
expect(result).toBe(null);
describe('returns null for weird source', () => {
it('calls reporter.verbose when undefined log level', async () => {
const source = {
one: 'thing',
another: 'thang',
};
const args = {};
const result = await resolveCloudinaryAssetData(
source,
args,
context,
info
);
expect(generateImageData).toBeCalledTimes(0);
expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(1);
expect(result).toBe(null);
});

it('calls reporter.verbose when log level = "verbose"', async () => {
const source = {
one: 'thing',
another: 'thang',
};
const args = {
logLevel: 'verbose',
};
const result = await resolveCloudinaryAssetData(
source,
args,
context,
info
);
expect(generateImageData).toBeCalledTimes(0);
expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(1);
expect(result).toBe(null);
});

it('does not call reporter.verbose when log level = "warn"', async () => {
const source = {
one: 'thing',
another: 'thang',
};
const args = {
logLevel: 'warn',
};
const result = await resolveCloudinaryAssetData(
source,
args,
context,
info
);
expect(generateImageData).toBeCalledTimes(0);
expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(0);
expect(result).toBe(null);
});
});

it('calls reporter.verbose and returns null for null source', async () => {
Expand Down Expand Up @@ -263,21 +303,42 @@ describe('resolveCloudinaryAssetData', () => {
expect(result).toBe(null);
});

it('calls reporter.warn and returns null for missing data', async () => {
const source = {
publicId: 'publicId',
one: 'thing',
};
const args = {};
const result = await resolveCloudinaryAssetData(
source,
args,
context,
info
);
expect(generateImageData).toBeCalledTimes(0);
expect(gatsbyUtilsMocks.reporter.warn).toBeCalledTimes(1);
expect(result).toBe(null);
describe('returns null for missing data', () => {
it('returns null for missing data when log level is undefined', async () => {
const source = {
publicId: 'publicId',
one: 'thing',
};
const args = {};
const result = await resolveCloudinaryAssetData(
source,
args,
context,
info
);
expect(generateImageData).toBeCalledTimes(0);
expect(gatsbyUtilsMocks.reporter.warn).toBeCalledTimes(1);
expect(result).toBe(null);
});

it('does not call reporter.warn when log level = "error"', async () => {
const source = {
publicId: 'publicId',
one: 'thing',
};
const args = {
logLevel: 'error',
};
const result = await resolveCloudinaryAssetData(
source,
args,
context,
info
);
expect(generateImageData).toBeCalledTimes(0);
expect(gatsbyUtilsMocks.reporter.warn).toBeCalledTimes(0);
expect(result).toBe(null);
});
});
});

Expand Down
25 changes: 25 additions & 0 deletions plugin/gatsby-plugin-image/resolver-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const LEVEL = {
verbose: 0,
info: 1,
warn: 2,
error: 3,
panic: 4,
panicOnBuild: 4,
};

exports.resolverReporter = ({ reporter, logLevel }) => {
const log = (level, message, ...rest) => {
if (!logLevel || LEVEL[level] >= LEVEL[logLevel]) {
reporter[level](message, ...rest);
}
};

return {
verbose: (...args) => log('verbose', ...args),
info: (...args) => log('info', ...args),
warn: (...args) => log('warn', ...args),
error: (...args) => log('error', ...args),
panic: (...args) => log('panic', ...args),
panicOnBuild: (...args) => log('panicOnBuild', ...args),
};
};
143 changes: 143 additions & 0 deletions plugin/gatsby-plugin-image/resolver-reporter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
const { resolverReporter } = require('./resolver-reporter');

const gatsbyUtilsMocks = {
reporter: {
panicOnBuild: jest.fn(),
panic: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
verbose: jest.fn(),
},
};

describe('resolverReporter', () => {
it('reports only panic', () => {
const reporter = resolverReporter({
reporter: gatsbyUtilsMocks.reporter,
logLevel: 'panic',
});

reporter.panicOnBuild('message');
reporter.panic('message');
reporter.error('message');
reporter.warn('message');
reporter.info('message');
reporter.verbose('message');

expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.panicOnBuild).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.error).toBeCalledTimes(0);
expect(gatsbyUtilsMocks.reporter.warn).toBeCalledTimes(0);
expect(gatsbyUtilsMocks.reporter.info).toBeCalledTimes(0);
expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(0);
});

it('reports only errors and above', () => {
const reporter = resolverReporter({
reporter: gatsbyUtilsMocks.reporter,
logLevel: 'error',
});

reporter.panicOnBuild('message');
reporter.panic('message');
reporter.error('message');
reporter.warn('message');
reporter.info('message');
reporter.verbose('message');

expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.panicOnBuild).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.error).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.warn).toBeCalledTimes(0);
expect(gatsbyUtilsMocks.reporter.info).toBeCalledTimes(0);
expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(0);
});

it('reports only warnings and above', () => {
const reporter = resolverReporter({
reporter: gatsbyUtilsMocks.reporter,
logLevel: 'warn',
});

reporter.panicOnBuild('message');
reporter.panic('message');
reporter.error('message');
reporter.warn('message');
reporter.info('message');
reporter.verbose('message');

expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.panicOnBuild).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.error).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.warn).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.info).toBeCalledTimes(0);
expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(0);
});

it('reports only info and above', () => {
const reporter = resolverReporter({
reporter: gatsbyUtilsMocks.reporter,
logLevel: 'info',
});

reporter.panicOnBuild('message');
reporter.panic('message');
reporter.error('message');
reporter.warn('message');
reporter.info('message');
reporter.verbose('message');

expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.panicOnBuild).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.error).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.warn).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.info).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(0);
});

describe('reports all', () => {
it('when log level is verbose', () => {
const reporter = resolverReporter({
reporter: gatsbyUtilsMocks.reporter,
logLevel: 'verbose',
});

reporter.panicOnBuild('message');
reporter.panic('message');
reporter.error('message');
reporter.warn('message');
reporter.info('message');
reporter.verbose('message');

expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.panicOnBuild).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.error).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.warn).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.info).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledWith('message');
});

it('when log level is undefined', () => {
const reporter = resolverReporter({
reporter: gatsbyUtilsMocks.reporter,
});

reporter.panicOnBuild('message');
reporter.panic('message');
reporter.error('message');
reporter.warn('message');
reporter.info('message');
reporter.verbose('message');

expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.panicOnBuild).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.error).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.warn).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.info).toBeCalledWith('message');
expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledWith('message');
});
});
});
3 changes: 3 additions & 0 deletions plugin/gatsby-plugin-image/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ exports.createGatsbyPluginImageResolver = (gatsbyUtils, pluginOptions) => {
type: 'Boolean',
defaultValue: true,
},
logLevel: {
type: 'String',
},
}
);
} catch (error) {
Expand Down

0 comments on commit b8cf805

Please sign in to comment.