Skip to content

Commit

Permalink
fix: JSON data parsing with extras (#691)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Sep 12, 2023
1 parent f9cbfd3 commit 3adb3cd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
15 changes: 3 additions & 12 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@ jobs:
outputs:
versions: ${{ steps.generate-matrix.outputs.versions }}
steps:
- name: Generate Node.js versions matrix
- name: Select 3 most recent LTS versions of Node.js
id: generate-matrix
run: |
sudo apt-get install -y lynx
lynx -dump https://endoflife.date/nodejs | grep -E -o '[0-9]+[( a-zA-Z]+LTS\)' | grep -E -o '([0-9]+)' > eol.list
cat eol.list
lts1=$(cat eol.list | head -1)
lts2=$(cat eol.list | head -2 | tail -1)
lts3=$(cat eol.list | head -3 | tail -1)
VERSIONS="[$lts1, $lts2, $lts3]"
echo "versions=${VERSIONS}" >> "$GITHUB_OUTPUT"
run: echo "versions=$(curl -s https://endoflife.date/api/nodejs.json | jq -c '[[.[] | select(.lts != false)][:3] | .[].cycle | tonumber]')" >> "$GITHUB_OUTPUT"


test:
needs:
Expand All @@ -33,8 +26,6 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm i -g npm
name: Update NPM
- run: npm install --no-package-lock
name: Install dev dependencies
- run: npm run build
Expand Down
22 changes: 14 additions & 8 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,22 +866,28 @@ async function formatConfigMarker (configsGetter, desiredMarker, defaultMarker)
function parseJsonData (output, entityName) {
if (!/\bresult=-1\b/.test(output) || !/\bdata="/.test(output)) {
log.debug(output);
throw new Error(`Cannot retrieve ${entityName} from the device. ` +
'Check the server log for more details');
throw new Error(
`Cannot retrieve ${entityName} from the device. ` +
'Check the server log for more details'
);
}
const match = /\bdata=(".+)/.exec(output);
const match = /\bdata="(.+)",?/.exec(output);
if (!match) {
log.debug(output);
throw new Error(`Cannot parse ${entityName} from the command output. ` +
'Check the server log for more details');
throw new Error(
`Cannot parse ${entityName} from the command output. ` +
'Check the server log for more details'
);
}
const jsonStr = _.trim(match[1]).replace(/(^")|("$)/g, '');
const jsonStr = _.trim(match[1]);
try {
return JSON.parse(jsonStr);
} catch (e) {
log.debug(jsonStr);
throw new Error(`Cannot parse ${entityName} from the resulting data string. ` +
'Check the server log for more details');
throw new Error(
`Cannot parse ${entityName} from the resulting data string. ` +
'Check the server log for more details'
);
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/unit/helper-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,15 @@ describe('helpers', withMocks({fs}, function (mocks) {
items.length.should.eql(2);
total.should.eql(2);
});
it('should parse JSON received from broadcast output having extras', function () {
const broadcastOutput = `
Broadcasting: Intent { act=io.appium.settings.sms.read flg=0x400000 (has extras) }
Broadcast completed: result=-1, data="{"items":[{"id":"2","address":"+123456789","date":"1581936422203","read":"0","status":"-1","type":"1","body":"\\"text message2\\""},{"id":"1","address":"+123456789","date":"1581936382740","read":"0","status":"-1","type":"1","body":"\\"text message\\""}],"total":2}", extras: Bundle[mParcelledData.dataSize=52]
`;
const {items, total} = parseJsonData(broadcastOutput, '');
items.length.should.eql(2);
total.should.eql(2);
});
it('should throw an error if json retrieval fails', function () {
const broadcastOutput = `
Broadcasting: Intent { act=io.appium.settings.sms.read flg=0x400000 (has extras) }
Expand Down

0 comments on commit 3adb3cd

Please sign in to comment.