From 3adb3cdb635d66696433da3963192d86d0cddda5 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Tue, 12 Sep 2023 09:37:06 +0200 Subject: [PATCH] fix: JSON data parsing with extras (#691) --- .github/workflows/unit-test.yml | 15 +++------------ lib/helpers.js | 22 ++++++++++++++-------- test/unit/helper-specs.js | 9 +++++++++ 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 14fba3b9..942eb0e5 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -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: @@ -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 diff --git a/lib/helpers.js b/lib/helpers.js index c92a4fff..2c59bac5 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -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' + ); } } diff --git a/test/unit/helper-specs.js b/test/unit/helper-specs.js index dac66169..7f1e9805 100644 --- a/test/unit/helper-specs.js +++ b/test/unit/helper-specs.js @@ -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) }