Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix no-empty issues #9202

Merged
merged 1 commit into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module.exports = {
'global-require': 'error',
'guard-for-in': 'error',
'no-case-declarations': 'error',
'no-empty': 'error',
'no-loop-func': 'error',
'no-useless-catch': 'error',
'no-useless-concat': 'error',
Expand Down
4 changes: 3 additions & 1 deletion app/scripts/migrations/002.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export default {
versionedData.data.config.provider.type = 'rpc'
versionedData.data.config.provider.rpcTarget = 'https://rpc.metamask.io/'
}
} catch (e) {}
} catch (_) {
// empty
}
return Promise.resolve(versionedData)
},
}
4 changes: 3 additions & 1 deletion app/scripts/migrations/003.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export default {
if (versionedData.data.config.provider.rpcTarget === oldTestRpc) {
versionedData.data.config.provider.rpcTarget = newTestRpc
}
} catch (e) {}
} catch (_) {
// empty
}
return Promise.resolve(versionedData)
},
}
4 changes: 3 additions & 1 deletion app/scripts/migrations/004.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export default {
break
// No default
}
} catch (_) {}
} catch (_) {
// empty
}
return Promise.resolve(safeVersionedData)
},
}
12 changes: 9 additions & 3 deletions development/sourcemap-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ async function validateSourcemapForFile ({ buildName }) {
try {
const filePath = path.join(__dirname, `/../dist/${platform}/`, `${buildName}`)
rawBuild = await fsAsync.readFile(filePath, 'utf8')
} catch (err) {}
} catch (_) {
// empty
}
if (!rawBuild) {
throw new Error(`SourcemapValidator - failed to load source file for "${buildName}"`)
}
Expand All @@ -58,12 +60,16 @@ async function validateSourcemapForFile ({ buildName }) {
try {
const filePath = path.join(__dirname, `/../dist/sourcemaps/`, `${buildName}.map`)
rawSourceMap = await fsAsync.readFile(filePath, 'utf8')
} catch (err) {}
} catch (_) {
// empty
}
// attempt to load in dev mode
try {
const filePath = path.join(__dirname, `/../dist/${platform}/`, `${buildName}.map`)
rawSourceMap = await fsAsync.readFile(filePath, 'utf8')
} catch (err) {}
} catch (_) {
// empty
}
if (!rawSourceMap) {
throw new Error(`SourcemapValidator - failed to load sourcemaps for "${buildName}"`)
}
Expand Down
50 changes: 10 additions & 40 deletions test/unit/app/controllers/preferences-controller-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,46 +478,16 @@ describe('preferences controller', function () {
assert.ok(assetImages[address], `set image correctly`)
})
it('should validate ERC20 asset correctly', async function () {
const validateSpy = sandbox.spy(preferencesController._validateERC20AssetParams)
try {
validateSpy({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABC', decimals: 0 })
} catch (e) {}
assert.equal(validateSpy.threw(), false, 'correct options object')
const validateSpyAddress = sandbox.spy(preferencesController._validateERC20AssetParams)
try {
validateSpyAddress({ symbol: 'ABC', decimals: 0 })
} catch (e) {}
assert.equal(validateSpyAddress.threw(), true, 'options object with no address')
const validateSpySymbol = sandbox.spy(preferencesController._validateERC20AssetParams)
try {
validateSpySymbol({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', decimals: 0 })
} catch (e) {}
assert.equal(validateSpySymbol.threw(), true, 'options object with no symbol')
const validateSpyDecimals = sandbox.spy(preferencesController._validateERC20AssetParams)
try {
validateSpyDecimals({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABC' })
} catch (e) {}
assert.equal(validateSpyDecimals.threw(), true, 'options object with no decimals')
const validateSpyInvalidSymbol = sandbox.spy(preferencesController._validateERC20AssetParams)
try {
validateSpyInvalidSymbol({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABCDEFGHI', decimals: 0 })
} catch (e) {}
assert.equal(validateSpyInvalidSymbol.threw(), true, 'options object with invalid symbol')
const validateSpyInvalidDecimals1 = sandbox.spy(preferencesController._validateERC20AssetParams)
try {
validateSpyInvalidDecimals1({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABCDEFGHI', decimals: -1 })
} catch (e) {}
assert.equal(validateSpyInvalidDecimals1.threw(), true, 'options object with decimals less than zero')
const validateSpyInvalidDecimals2 = sandbox.spy(preferencesController._validateERC20AssetParams)
try {
validateSpyInvalidDecimals2({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABCDEFGHI', decimals: 38 })
} catch (e) {}
assert.equal(validateSpyInvalidDecimals2.threw(), true, 'options object with decimals more than 36')
const validateSpyInvalidAddress = sandbox.spy(preferencesController._validateERC20AssetParams)
try {
validateSpyInvalidAddress({ rawAddress: '0x123', symbol: 'ABC', decimals: 0 })
} catch (e) {}
assert.equal(validateSpyInvalidAddress.threw(), true, 'options object with address invalid')
const validate = preferencesController._validateERC20AssetParams

assert.doesNotThrow(() => validate({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABC', decimals: 0 }))
assert.throws(() => validate({ symbol: 'ABC', decimals: 0 }), 'missing address should fail')
assert.throws(() => validate({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', decimals: 0 }), 'missing symbol should fail')
assert.throws(() => validate({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABC' }), 'missing decimals should fail')
assert.throws(() => validate({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABCDEFGHI', decimals: 0 }), 'invalid symbol should fail')
assert.throws(() => validate({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABCDEFGHI', decimals: -1 }), 'decimals < 0 should fail')
assert.throws(() => validate({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABCDEFGHI', decimals: 38 }), 'decimals > 36 should fail')
Comment on lines +488 to +489
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welp; these two tests have always been broken. They'd fail because of the symbol, not because of the decimals.

Suggested change
assert.throws(() => validate({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABCDEFGHI', decimals: -1 }), 'decimals < 0 should fail')
assert.throws(() => validate({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABCDEFGHI', decimals: 38 }), 'decimals > 36 should fail')
assert.throws(() => validate({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABC', decimals: -1 }), 'decimals < 0 should fail')
assert.throws(() => validate({ rawAddress: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07', symbol: 'ABC', decimals: 38 }), 'decimals > 36 should fail')

Out-of-scope for this PR, just mentioning it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update these separately 👍

assert.throws(() => validate({ rawAddress: '0x123', symbol: 'ABC', decimals: 0 }), 'invalid address should fail')
})
})

Expand Down
5 changes: 3 additions & 2 deletions ui/app/helpers/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ export function shortenBalance (balance, decimalsToKeep = 1) {
export function normalizeToWei (amount, currency) {
try {
return amount.mul(bnTable.wei).div(bnTable[currency])
} catch (e) {}
return amount
} catch (e) {
return amount
}
}

export function normalizeEthStringToWei (str) {
Expand Down