2017-06-05 16:02:39 +03:00
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
2015-11-18 17:16:17 +02:00
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2015-02-20 21:02:10 +02:00
2019-10-17 23:40:30 +03:00
import os
2019-10-18 22:00:28 +03:00
import re
2020-06-15 22:05:59 +03:00
import tarfile
2019-10-17 23:40:30 +03:00
2019-10-17 00:17:16 +03:00
import jsondiff
2015-02-24 20:11:57 +02:00
import pytest
2019-10-15 13:05:56 +03:00
from platformio . compat import WINDOWS
2019-10-17 00:17:16 +03:00
from platformio . package . manifest import parser
from platformio . package . manifest . schema import ManifestSchema , ManifestValidationError
2015-02-20 21:02:10 +02:00
2019-09-30 17:59:06 +03:00
def test_library_json_parser ( ) :
contents = """
{
" name " : " TestPackage " ,
" keywords " : " kw1, KW2, kw3 " ,
" platforms " : [ " atmelavr " , " espressif " ] ,
2019-12-22 23:45:12 +02:00
" repository " : {
" type " : " git " ,
" url " : " http://github.com/username/repo/ "
} ,
2019-09-30 17:59:06 +03:00
" url " : " http://old.url.format " ,
" exclude " : [ " .gitignore " , " tests " ] ,
2019-10-24 13:42:46 +03:00
" include " : " mylib " ,
" build " : {
" flags " : [ " -DHELLO " ]
} ,
2020-01-10 21:28:19 +02:00
" examples " : [ " examples/*/*.pde " ] ,
2020-02-05 00:04:16 +02:00
" dependencies " : {
" deps1 " : " 1.2.0 " ,
" deps2 " : " https://github.com/username/package.git " ,
" @owner/deps3 " : " ^2.1.3 "
} ,
2019-10-24 13:42:46 +03:00
" customField " : " Custom Value "
2019-09-30 17:59:06 +03:00
}
"""
2020-02-05 14:34:40 +02:00
raw_data = parser . LibraryJsonManifestParser ( contents ) . as_dict ( )
raw_data [ " dependencies " ] = sorted ( raw_data [ " dependencies " ] , key = lambda a : a [ " name " ] )
2019-10-17 00:17:16 +03:00
assert not jsondiff . diff (
2020-02-05 14:34:40 +02:00
raw_data ,
2019-09-30 17:59:06 +03:00
{
" name " : " TestPackage " ,
" platforms " : [ " atmelavr " , " espressif8266 " ] ,
2019-12-22 23:45:12 +02:00
" repository " : {
" type " : " git " ,
" url " : " https://github.com/username/repo.git " ,
} ,
2019-09-30 17:59:06 +03:00
" export " : { " exclude " : [ " .gitignore " , " tests " ] , " include " : [ " mylib " ] } ,
" keywords " : [ " kw1 " , " kw2 " , " kw3 " ] ,
" homepage " : " http://old.url.format " ,
2019-10-24 13:42:46 +03:00
" build " : { " flags " : [ " -DHELLO " ] } ,
2020-02-05 00:04:16 +02:00
" dependencies " : [
2020-02-05 14:34:40 +02:00
{ " name " : " @owner/deps3 " , " version " : " ^2.1.3 " } ,
2020-02-05 00:04:16 +02:00
{ " name " : " deps1 " , " version " : " 1.2.0 " } ,
{ " name " : " deps2 " , " version " : " https://github.com/username/package.git " } ,
] ,
2019-10-24 13:42:46 +03:00
" customField " : " Custom Value " ,
2020-02-05 15:31:32 +02:00
} ,
2019-09-30 17:59:06 +03:00
)
2015-02-20 21:02:10 +02:00
2019-10-01 16:13:25 +03:00
contents = """
{
" keywords " : [ " sound " , " audio " , " music " , " SD " , " card " , " playback " ] ,
" frameworks " : " arduino " ,
" platforms " : " atmelavr " ,
" export " : {
" exclude " : " audio_samples "
2020-02-05 00:04:16 +02:00
} ,
" dependencies " : [
{ " name " : " deps1 " , " version " : " 1.0.0 " } ,
2020-06-27 12:46:04 +03:00
{ " name " : " @owner/deps2 " , " version " : " 1.0.0 " , " platforms " : " * " , " frameworks " : " arduino, espidf " } ,
2020-02-05 00:04:16 +02:00
{ " name " : " deps3 " , " version " : " 1.0.0 " , " platforms " : [ " ststm32 " , " sifive " ] }
]
2019-10-01 16:13:25 +03:00
}
"""
2020-02-05 15:43:39 +02:00
raw_data = parser . LibraryJsonManifestParser ( contents ) . as_dict ( )
raw_data [ " dependencies " ] = sorted ( raw_data [ " dependencies " ] , key = lambda a : a [ " name " ] )
2019-10-17 00:17:16 +03:00
assert not jsondiff . diff (
2020-02-05 15:43:39 +02:00
raw_data ,
2019-10-01 16:13:25 +03:00
{
" keywords " : [ " sound " , " audio " , " music " , " sd " , " card " , " playback " ] ,
" frameworks " : [ " arduino " ] ,
" export " : { " exclude " : [ " audio_samples " ] } ,
" platforms " : [ " atmelavr " ] ,
2020-02-05 00:04:16 +02:00
" dependencies " : [
{
" name " : " @owner/deps2 " ,
" version " : " 1.0.0 " ,
2020-06-27 12:46:04 +03:00
" platforms " : [ " * " ] ,
2020-02-05 00:04:16 +02:00
" frameworks " : [ " arduino " , " espidf " ] ,
} ,
2020-02-05 15:43:39 +02:00
{ " name " : " deps1 " , " version " : " 1.0.0 " } ,
2020-02-05 00:04:16 +02:00
{
" name " : " deps3 " ,
" version " : " 1.0.0 " ,
" platforms " : [ " ststm32 " , " sifive " ] ,
} ,
] ,
2019-10-17 00:17:16 +03:00
} ,
2019-10-01 16:13:25 +03:00
)
2020-02-18 21:55:01 +02:00
raw_data = parser . LibraryJsonManifestParser (
' { " dependencies " : [ " dep1 " , " dep2 " , " @owner/dep3 " ]} '
) . as_dict ( )
raw_data [ " dependencies " ] = sorted ( raw_data [ " dependencies " ] , key = lambda a : a [ " name " ] )
assert not jsondiff . diff (
raw_data ,
{
" dependencies " : [
{ " name " : " @owner/dep3 " } ,
{ " name " : " dep1 " } ,
{ " name " : " dep2 " } ,
] ,
} ,
)
2020-02-05 00:04:16 +02:00
# broken dependencies
with pytest . raises ( parser . ManifestParserError ) :
2020-02-05 15:43:39 +02:00
parser . LibraryJsonManifestParser ( { " dependencies " : [ " deps1 " , " deps2 " ] } )
2020-02-05 00:04:16 +02:00
2017-03-08 17:24:58 +02:00
2019-09-30 17:59:06 +03:00
def test_module_json_parser ( ) :
contents = """
{
" author " : " Name Surname <name@surname.com> " ,
" description " : " This is Yotta library " ,
" homepage " : " https://yottabuild.org " ,
" keywords " : [
" mbed " ,
" Yotta "
] ,
" licenses " : [
{
" type " : " Apache-2.0 " ,
" url " : " https://spdx.org/licenses/Apache-2.0 "
}
] ,
" name " : " YottaLibrary " ,
" repository " : {
" type " : " git " ,
" url " : " git@github.com:username/repo.git "
} ,
2019-10-24 13:42:46 +03:00
" version " : " 1.2.3 " ,
2020-03-03 23:10:19 +02:00
" dependencies " : {
" usefulmodule " : " ^1.2.3 " ,
" simplelog " : " ARMmbed/simplelog#~0.0.1 "
} ,
2019-10-24 13:42:46 +03:00
" customField " : " Custom Value "
2019-09-30 17:59:06 +03:00
}
"""
2019-10-24 13:42:46 +03:00
2020-03-04 18:14:51 +02:00
raw_data = parser . ModuleJsonManifestParser ( contents ) . as_dict ( )
raw_data [ " dependencies " ] = sorted ( raw_data [ " dependencies " ] , key = lambda a : a [ " name " ] )
2019-10-17 00:17:16 +03:00
assert not jsondiff . diff (
2020-03-04 18:14:51 +02:00
raw_data ,
2019-09-30 17:59:06 +03:00
{
" name " : " YottaLibrary " ,
" description " : " This is Yotta library " ,
" homepage " : " https://yottabuild.org " ,
" keywords " : [ " mbed " , " Yotta " ] ,
" license " : " Apache-2.0 " ,
" platforms " : [ " * " ] ,
" frameworks " : [ " mbed " ] ,
" export " : { " exclude " : [ " tests " , " test " , " *.doxyfile " , " *.pdf " ] } ,
2019-10-17 00:17:16 +03:00
" authors " : [ { " email " : " name@surname.com " , " name " : " Name Surname " } ] ,
2019-09-30 17:59:06 +03:00
" version " : " 1.2.3 " ,
2019-10-24 13:42:46 +03:00
" repository " : { " type " : " git " , " url " : " git@github.com:username/repo.git " } ,
2020-03-03 23:10:19 +02:00
" dependencies " : [
{
" name " : " simplelog " ,
" version " : " ARMmbed/simplelog#~0.0.1 " ,
" frameworks " : [ " mbed " ] ,
} ,
2020-03-04 18:14:51 +02:00
{ " name " : " usefulmodule " , " version " : " ^1.2.3 " , " frameworks " : [ " mbed " ] } ,
2020-03-03 23:10:19 +02:00
] ,
2019-10-24 13:42:46 +03:00
" customField " : " Custom Value " ,
2019-10-17 00:17:16 +03:00
} ,
2019-09-30 17:59:06 +03:00
)
2015-05-29 18:34:21 +03:00
2015-02-24 20:11:57 +02:00
2019-09-30 17:59:06 +03:00
def test_library_properties_parser ( ) :
# Base
contents = """
name = TestPackage
version = 1.2 .3
author = SomeAuthor < info AT author . com >
sentence = This is Arduino library
2019-10-24 13:42:46 +03:00
customField = Custom Value
2020-02-05 00:04:16 +02:00
depends = First Library ( = 2.0 .0 ) , Second Library ( > = 1.2 .0 ) , Third
2020-07-06 14:17:00 +03:00
ignore_empty_field =
2019-09-30 17:59:06 +03:00
"""
2020-02-05 15:43:39 +02:00
raw_data = parser . LibraryPropertiesManifestParser ( contents ) . as_dict ( )
raw_data [ " dependencies " ] = sorted ( raw_data [ " dependencies " ] , key = lambda a : a [ " name " ] )
2019-10-17 00:17:16 +03:00
assert not jsondiff . diff (
2020-02-05 15:43:39 +02:00
raw_data ,
2019-09-30 17:59:06 +03:00
{
" name " : " TestPackage " ,
" version " : " 1.2.3 " ,
" description " : " This is Arduino library " ,
2019-10-24 13:42:46 +03:00
" sentence " : " This is Arduino library " ,
2019-09-30 17:59:06 +03:00
" platforms " : [ " * " ] ,
" frameworks " : [ " arduino " ] ,
" export " : {
2019-10-17 00:17:16 +03:00
" exclude " : [ " extras " , " docs " , " tests " , " test " , " *.doxyfile " , " *.pdf " ]
2019-09-30 17:59:06 +03:00
} ,
2019-10-17 00:17:16 +03:00
" authors " : [ { " email " : " info@author.com " , " name " : " SomeAuthor " } ] ,
2019-09-30 17:59:06 +03:00
" keywords " : [ " uncategorized " ] ,
2019-10-24 13:42:46 +03:00
" customField " : " Custom Value " ,
2020-02-05 00:04:16 +02:00
" depends " : " First Library (=2.0.0), Second Library (>=1.2.0), Third " ,
" dependencies " : [
{
" name " : " First Library " ,
" version " : " =2.0.0 " ,
" frameworks " : [ " arduino " ] ,
} ,
{
" name " : " Second Library " ,
" version " : " >=1.2.0 " ,
" frameworks " : [ " arduino " ] ,
} ,
{ " name " : " Third " , " frameworks " : [ " arduino " ] } ,
] ,
2019-10-17 00:17:16 +03:00
} ,
2019-09-30 17:59:06 +03:00
)
# Platforms ALL
2019-10-04 18:30:48 +03:00
data = parser . LibraryPropertiesManifestParser (
" architectures=* \n " + contents
) . as_dict ( )
assert data [ " platforms " ] == [ " * " ]
2019-12-29 17:43:50 +02:00
2019-09-30 17:59:06 +03:00
# Platforms specific
2019-10-04 18:30:48 +03:00
data = parser . LibraryPropertiesManifestParser (
" architectures=avr, esp32 \n " + contents
) . as_dict ( )
assert data [ " platforms " ] == [ " atmelavr " , " espressif32 " ]
2019-09-30 17:59:06 +03:00
# Remote URL
2019-10-04 18:30:48 +03:00
data = parser . LibraryPropertiesManifestParser (
2019-09-30 17:59:06 +03:00
contents ,
remote_url = (
" https://raw.githubusercontent.com/username/reponame/master/ "
" libraries/TestPackage/library.properties "
) ,
2019-10-04 18:30:48 +03:00
) . as_dict ( )
assert data [ " export " ] == {
2019-09-30 17:59:06 +03:00
" exclude " : [ " extras " , " docs " , " tests " , " test " , " *.doxyfile " , " *.pdf " ] ,
2019-10-17 21:19:04 +03:00
" include " : [ " libraries/TestPackage " ] ,
2019-09-30 17:59:06 +03:00
}
2019-10-04 18:30:48 +03:00
assert data [ " repository " ] == {
2019-12-22 23:45:12 +02:00
" url " : " https://github.com/username/reponame.git " ,
2019-10-04 18:30:48 +03:00
" type " : " git " ,
}
2019-09-30 17:59:06 +03:00
2019-12-29 17:43:50 +02:00
# Home page
2019-10-04 18:30:48 +03:00
data = parser . LibraryPropertiesManifestParser (
2019-09-30 17:59:06 +03:00
" url=https://github.com/username/reponame.git \n " + contents
2019-10-04 18:30:48 +03:00
) . as_dict ( )
assert data [ " repository " ] == {
2019-09-30 17:59:06 +03:00
" type " : " git " ,
" url " : " https://github.com/username/reponame.git " ,
}
2019-12-29 17:43:50 +02:00
# Author + Maintainer
data = parser . LibraryPropertiesManifestParser (
"""
author = Rocket Scream Electronics
maintainer = Rocket Scream Electronics
"""
) . as_dict ( )
assert data [ " authors " ] == [
{ " name " : " Rocket Scream Electronics " , " maintainer " : True }
]
2019-09-30 17:59:06 +03:00
2019-10-17 12:38:35 +03:00
def test_library_json_schema ( ) :
2019-09-30 17:59:06 +03:00
contents = """
{
" name " : " ArduinoJson " ,
" keywords " : " JSON, rest, http, web " ,
" description " : " An elegant and efficient JSON library for embedded systems " ,
" homepage " : " https://arduinojson.org " ,
" repository " : {
" type " : " git " ,
" url " : " https://github.com/bblanchon/ArduinoJson.git "
} ,
" version " : " 6.12.0 " ,
" authors " : {
" name " : " Benoit Blanchon " ,
" url " : " https://blog.benoitblanchon.fr "
} ,
2019-12-20 20:20:09 +02:00
" downloadUrl " : " https://example.com/package.tar.gz " ,
2019-09-30 17:59:06 +03:00
" exclude " : [
" fuzzing " ,
" scripts " ,
" test " ,
" third-party "
] ,
" frameworks " : " arduino " ,
" platforms " : " * " ,
2019-10-01 17:37:11 +03:00
" license " : " MIT " ,
2019-10-02 11:04:29 +03:00
" examples " : [
{
" name " : " JsonConfigFile " ,
2019-10-01 17:37:11 +03:00
" base " : " examples/JsonConfigFile " ,
" files " : [ " JsonConfigFile.ino " ]
} ,
2019-10-02 11:04:29 +03:00
{
" name " : " JsonHttpClient " ,
2019-10-01 17:37:11 +03:00
" base " : " examples/JsonHttpClient " ,
" files " : [ " JsonHttpClient.ino " ]
}
2020-02-05 00:04:16 +02:00
] ,
" dependencies " : [
{ " name " : " deps1 " , " version " : " 1.0.0 " } ,
{ " name " : " @owner/deps2 " , " version " : " 1.0.0 " , " frameworks " : " arduino " } ,
{ " name " : " deps3 " , " version " : " 1.0.0 " , " platforms " : [ " ststm32 " , " sifive " ] }
2019-10-02 11:04:29 +03:00
]
2019-09-30 17:59:06 +03:00
}
"""
2019-10-17 00:17:16 +03:00
raw_data = parser . ManifestParserFactory . new (
2019-10-01 00:11:31 +03:00
contents , parser . ManifestFileType . LIBRARY_JSON
2019-10-04 18:30:48 +03:00
) . as_dict ( )
2020-02-05 15:43:39 +02:00
raw_data [ " dependencies " ] = sorted ( raw_data [ " dependencies " ] , key = lambda a : a [ " name " ] )
2019-10-17 00:17:16 +03:00
2019-12-29 13:13:04 +02:00
data = ManifestSchema ( ) . load_manifest ( raw_data )
2019-10-17 00:17:16 +03:00
assert data [ " repository " ] [ " url " ] == " https://github.com/bblanchon/ArduinoJson.git "
assert data [ " examples " ] [ 1 ] [ " base " ] == " examples/JsonHttpClient "
assert data [ " examples " ] [ 1 ] [ " files " ] == [ " JsonHttpClient.ino " ]
assert not jsondiff . diff (
data ,
{
2019-09-30 17:59:06 +03:00
" name " : " ArduinoJson " ,
" keywords " : [ " json " , " rest " , " http " , " web " ] ,
" description " : " An elegant and efficient JSON library for embedded systems " ,
" homepage " : " https://arduinojson.org " ,
" repository " : {
" url " : " https://github.com/bblanchon/ArduinoJson.git " ,
" type " : " git " ,
} ,
" version " : " 6.12.0 " ,
2019-09-30 19:44:03 +03:00
" authors " : [
2019-10-17 00:17:16 +03:00
{ " name " : " Benoit Blanchon " , " url " : " https://blog.benoitblanchon.fr " }
2019-09-30 19:44:03 +03:00
] ,
2019-12-20 20:20:09 +02:00
" downloadUrl " : " https://example.com/package.tar.gz " ,
2019-10-17 00:17:16 +03:00
" export " : { " exclude " : [ " fuzzing " , " scripts " , " test " , " third-party " ] } ,
2019-09-30 17:59:06 +03:00
" frameworks " : [ " arduino " ] ,
" platforms " : [ " * " ] ,
" license " : " MIT " ,
2019-10-02 11:04:29 +03:00
" examples " : [
{
" name " : " JsonConfigFile " ,
2019-10-01 17:37:11 +03:00
" base " : " examples/JsonConfigFile " ,
" files " : [ " JsonConfigFile.ino " ] ,
} ,
2019-10-02 11:04:29 +03:00
{
" name " : " JsonHttpClient " ,
2019-10-01 17:37:11 +03:00
" base " : " examples/JsonHttpClient " ,
" files " : [ " JsonHttpClient.ino " ] ,
} ,
2019-10-02 11:04:29 +03:00
] ,
2020-02-05 00:04:16 +02:00
" dependencies " : [
{ " name " : " @owner/deps2 " , " version " : " 1.0.0 " , " frameworks " : [ " arduino " ] } ,
2020-02-05 15:43:39 +02:00
{ " name " : " deps1 " , " version " : " 1.0.0 " } ,
2020-02-05 00:04:16 +02:00
{
" name " : " deps3 " ,
" version " : " 1.0.0 " ,
" platforms " : [ " ststm32 " , " sifive " ] ,
} ,
] ,
2019-10-17 00:17:16 +03:00
} ,
2019-09-30 17:59:06 +03:00
)
2019-09-30 23:45:03 +03:00
2020-02-05 15:43:39 +02:00
# legacy dependencies format
contents = """
{
" name " : " DallasTemperature " ,
" version " : " 3.8.0 " ,
" dependencies " :
{
" name " : " OneWire " ,
" authors " : " Paul Stoffregen " ,
" frameworks " : " arduino "
}
}
"""
raw_data = parser . LibraryJsonManifestParser ( contents ) . as_dict ( )
data = ManifestSchema ( ) . load_manifest ( raw_data )
assert not jsondiff . diff (
data ,
{
" name " : " DallasTemperature " ,
" version " : " 3.8.0 " ,
" dependencies " : [
{
" name " : " OneWire " ,
" authors " : [ " Paul Stoffregen " ] ,
" frameworks " : [ " arduino " ] ,
}
] ,
} ,
)
2019-09-30 23:45:03 +03:00
2019-10-17 12:38:35 +03:00
def test_library_properties_schema ( ) :
2019-09-30 23:45:03 +03:00
contents = """
name = U8glib
version = 1.19 .1
author = oliver < olikraus @gmail.com >
maintainer = oliver < olikraus @gmail.com >
sentence = A library for monochrome TFTs and OLEDs
paragraph = Supported display controller : SSD1306 , SSD1309 , SSD1322 , SSD1325
category = Display
url = https : / / github . com / olikraus / u8glib
architectures = avr , sam
2020-02-05 00:04:16 +02:00
depends = First Library ( = 2.0 .0 ) , Second Library ( > = 1.2 .0 ) , Third
2019-09-30 23:45:03 +03:00
"""
2019-10-17 00:17:16 +03:00
raw_data = parser . ManifestParserFactory . new (
2019-09-30 23:45:03 +03:00
contents , parser . ManifestFileType . LIBRARY_PROPERTIES
2019-10-04 18:30:48 +03:00
) . as_dict ( )
2020-02-05 15:43:39 +02:00
raw_data [ " dependencies " ] = sorted ( raw_data [ " dependencies " ] , key = lambda a : a [ " name " ] )
2019-10-17 00:17:16 +03:00
2019-12-29 13:13:04 +02:00
data = ManifestSchema ( ) . load_manifest ( raw_data )
2019-10-17 00:17:16 +03:00
assert not jsondiff . diff (
data ,
{
2019-09-30 23:45:03 +03:00
" description " : (
" A library for monochrome TFTs and OLEDs. Supported display "
" controller: SSD1306, SSD1309, SSD1322, SSD1325 "
) ,
2019-12-22 23:45:12 +02:00
" repository " : {
" url " : " https://github.com/olikraus/u8glib.git " ,
" type " : " git " ,
} ,
2019-09-30 23:45:03 +03:00
" frameworks " : [ " arduino " ] ,
" platforms " : [ " atmelavr " , " atmelsam " ] ,
" version " : " 1.19.1 " ,
" export " : {
2019-10-17 00:17:16 +03:00
" exclude " : [ " extras " , " docs " , " tests " , " test " , " *.doxyfile " , " *.pdf " ]
2019-09-30 23:45:03 +03:00
} ,
" authors " : [
2019-10-17 00:17:16 +03:00
{ " maintainer " : True , " email " : " olikraus@gmail.com " , " name " : " oliver " }
2019-09-30 23:45:03 +03:00
] ,
" keywords " : [ " display " ] ,
" name " : " U8glib " ,
2020-02-05 00:04:16 +02:00
" dependencies " : [
{
" name " : " First Library " ,
" version " : " =2.0.0 " ,
" frameworks " : [ " arduino " ] ,
} ,
{
" name " : " Second Library " ,
" version " : " >=1.2.0 " ,
" frameworks " : [ " arduino " ] ,
} ,
{ " name " : " Third " , " frameworks " : [ " arduino " ] } ,
] ,
2019-10-17 00:17:16 +03:00
} ,
2019-09-30 23:45:03 +03:00
)
2019-10-04 23:52:06 +03:00
# Broken fields
contents = """
name = Mozzi
version = 1.0 .3
author = Tim Barrass and contributors as documented in source , and at https : / / github . com / sensorium / Mozzi / graphs / contributors
maintainer = Tim Barrass < faveflave @gmail.com >
sentence = Sound synthesis library for Arduino
paragraph = With Mozzi , you can construct sounds using familiar synthesis units like oscillators , delays , filters and envelopes .
category = Signal Input / Output
url = https : / / sensorium . github . io / Mozzi /
architectures = *
dot_a_linkage = false
includes = MozziGuts . h
"""
2019-10-17 00:17:16 +03:00
raw_data = parser . ManifestParserFactory . new (
2019-10-04 23:52:06 +03:00
contents ,
parser . ManifestFileType . LIBRARY_PROPERTIES ,
remote_url = (
" https://raw.githubusercontent.com/sensorium/Mozzi/ "
" master/library.properties "
) ,
) . as_dict ( )
2019-10-17 00:17:16 +03:00
2019-12-29 13:13:04 +02:00
try :
ManifestSchema ( ) . load_manifest ( raw_data )
except ManifestValidationError as e :
data = e . valid_data
errors = e . messages
2019-10-17 00:17:16 +03:00
assert errors [ " authors " ]
assert not jsondiff . diff (
data ,
{
2019-10-04 23:52:06 +03:00
" name " : " Mozzi " ,
" version " : " 1.0.3 " ,
" description " : (
" Sound synthesis library for Arduino. With Mozzi, you can construct "
" sounds using familiar synthesis units like oscillators, delays, "
" filters and envelopes. "
) ,
2019-12-22 23:45:12 +02:00
" repository " : {
" url " : " https://github.com/sensorium/Mozzi.git " ,
" type " : " git " ,
} ,
2019-10-04 23:52:06 +03:00
" platforms " : [ " * " ] ,
" frameworks " : [ " arduino " ] ,
" export " : {
2019-10-17 00:17:16 +03:00
" exclude " : [ " extras " , " docs " , " tests " , " test " , " *.doxyfile " , " *.pdf " ]
2019-10-04 23:52:06 +03:00
} ,
" authors " : [
{
" maintainer " : True ,
" email " : " faveflave@gmail.com " ,
" name " : " Tim Barrass " ,
}
] ,
" keywords " : [ " signal " , " input " , " output " ] ,
" homepage " : " https://sensorium.github.io/Mozzi/ " ,
2019-10-17 00:17:16 +03:00
} ,
2019-10-04 23:52:06 +03:00
)
2019-09-30 23:45:03 +03:00
2019-10-17 12:38:35 +03:00
def test_platform_json_schema ( ) :
2019-10-01 22:03:23 +03:00
contents = """
{
" name " : " atmelavr " ,
" title " : " Atmel AVR " ,
" description " : " Atmel AVR 8- and 32-bit MCUs deliver a unique combination of performance, power efficiency and design flexibility. Optimized to speed time to market-and easily adapt to new ones-they are based on the industrys most code-efficient architecture for C and assembly programming. " ,
2020-06-26 19:49:25 +03:00
" keywords " : " arduino, atmel, avr " ,
" homepage " : " http://www.atmel.com/products/microcontrollers/avr/default.aspx " ,
2019-10-01 22:03:23 +03:00
" license " : " Apache-2.0 " ,
" engines " : {
" platformio " : " <5 "
} ,
" repository " : {
" type " : " git " ,
" url " : " https://github.com/platformio/platform-atmelavr.git "
} ,
" version " : " 1.15.0 " ,
" frameworks " : {
" arduino " : {
" package " : " framework-arduinoavr " ,
" script " : " builder/frameworks/arduino.py "
} ,
" simba " : {
" package " : " framework-simba " ,
" script " : " builder/frameworks/simba.py "
}
} ,
" packages " : {
" toolchain-atmelavr " : {
" type " : " toolchain " ,
" version " : " ~1.50400.0 "
} ,
" framework-arduinoavr " : {
" type " : " framework " ,
" optional " : true ,
" version " : " ~4.2.0 "
} ,
" tool-avrdude " : {
" type " : " uploader " ,
" optional " : true ,
" version " : " ~1.60300.0 "
}
}
}
"""
2019-10-17 00:17:16 +03:00
raw_data = parser . ManifestParserFactory . new (
2019-10-01 22:03:23 +03:00
contents , parser . ManifestFileType . PLATFORM_JSON
2019-10-04 18:30:48 +03:00
) . as_dict ( )
2019-10-17 12:38:35 +03:00
raw_data [ " frameworks " ] = sorted ( raw_data [ " frameworks " ] )
2020-02-05 15:43:39 +02:00
raw_data [ " dependencies " ] = sorted ( raw_data [ " dependencies " ] , key = lambda a : a [ " name " ] )
2019-12-29 13:13:04 +02:00
data = ManifestSchema ( ) . load_manifest ( raw_data )
2019-10-17 00:17:16 +03:00
assert not jsondiff . diff (
data ,
{
2019-10-01 22:03:23 +03:00
" name " : " atmelavr " ,
" title " : " Atmel AVR " ,
" description " : (
" Atmel AVR 8- and 32-bit MCUs deliver a unique combination of "
" performance, power efficiency and design flexibility. Optimized to "
" speed time to market-and easily adapt to new ones-they are based "
" on the industrys most code-efficient architecture for C and "
" assembly programming. "
) ,
2020-06-26 19:49:25 +03:00
" keywords " : [ " arduino " , " atmel " , " avr " ] ,
" homepage " : " http://www.atmel.com/products/microcontrollers/avr/default.aspx " ,
2019-10-01 22:03:23 +03:00
" license " : " Apache-2.0 " ,
" repository " : {
" url " : " https://github.com/platformio/platform-atmelavr.git " ,
" type " : " git " ,
} ,
2019-10-17 12:38:35 +03:00
" frameworks " : sorted ( [ " arduino " , " simba " ] ) ,
2019-10-01 22:03:23 +03:00
" version " : " 1.15.0 " ,
2020-02-05 00:04:16 +02:00
" dependencies " : [
{ " name " : " framework-arduinoavr " , " version " : " ~4.2.0 " } ,
{ " name " : " tool-avrdude " , " version " : " ~1.60300.0 " } ,
2020-02-05 15:43:39 +02:00
{ " name " : " toolchain-atmelavr " , " version " : " ~1.50400.0 " } ,
2020-02-05 00:04:16 +02:00
] ,
2019-10-17 18:48:59 +03:00
} ,
2019-10-01 22:03:23 +03:00
)
2019-10-17 12:38:35 +03:00
def test_package_json_schema ( ) :
2019-10-01 22:03:23 +03:00
contents = """
{
" name " : " tool-scons " ,
" description " : " SCons software construction tool " ,
2020-06-27 21:42:13 +03:00
" keywords " : " SCons, build " ,
2020-06-29 15:06:21 +03:00
" homepage " : " http://www.scons.org " ,
" system " : [ " linux_armv6l " , " linux_armv7l " , " linux_armv8l " ] ,
2019-10-01 22:03:23 +03:00
" version " : " 3.30101.0 "
}
"""
2019-10-17 00:17:16 +03:00
raw_data = parser . ManifestParserFactory . new (
2019-10-01 22:03:23 +03:00
contents , parser . ManifestFileType . PACKAGE_JSON
2019-10-04 18:30:48 +03:00
) . as_dict ( )
2019-10-17 00:17:16 +03:00
2019-12-29 13:13:04 +02:00
data = ManifestSchema ( ) . load_manifest ( raw_data )
2019-10-17 00:17:16 +03:00
assert not jsondiff . diff (
data ,
{
2019-10-01 22:03:23 +03:00
" name " : " tool-scons " ,
" description " : " SCons software construction tool " ,
2020-06-27 21:42:13 +03:00
" keywords " : [ " scons " , " build " ] ,
2019-10-01 22:03:23 +03:00
" homepage " : " http://www.scons.org " ,
2020-06-29 15:06:21 +03:00
" system " : [ " linux_armv6l " , " linux_armv7l " , " linux_armv8l " ] ,
2019-10-01 22:03:23 +03:00
" version " : " 3.30101.0 " ,
2019-10-17 00:17:16 +03:00
} ,
2019-10-01 22:03:23 +03:00
)
mp = parser . ManifestParserFactory . new (
' { " system " : " * " } ' , parser . ManifestFileType . PACKAGE_JSON
)
assert " system " not in mp . as_dict ( )
mp = parser . ManifestParserFactory . new (
2019-10-24 15:10:11 +03:00
' { " system " : " all " } ' , parser . ManifestFileType . PACKAGE_JSON
)
assert " system " not in mp . as_dict ( )
mp = parser . ManifestParserFactory . new (
2019-10-01 22:03:23 +03:00
' { " system " : " darwin_x86_64 " } ' , parser . ManifestFileType . PACKAGE_JSON
)
assert mp . as_dict ( ) [ " system " ] == [ " darwin_x86_64 " ]
2019-10-03 16:14:51 +03:00
def test_parser_from_dir ( tmpdir_factory ) :
pkg_dir = tmpdir_factory . mktemp ( " package " )
2020-01-09 23:32:55 +02:00
pkg_dir . join ( " package.json " ) . write ( ' { " name " : " package.json " } ' )
2019-10-03 16:14:51 +03:00
pkg_dir . join ( " library.json " ) . write ( ' { " name " : " library.json " } ' )
pkg_dir . join ( " library.properties " ) . write ( " name=library.properties " )
data = parser . ManifestParserFactory . new_from_dir ( str ( pkg_dir ) ) . as_dict ( )
assert data [ " name " ] == " library.json "
data = parser . ManifestParserFactory . new_from_dir (
str ( pkg_dir ) , remote_url = " http://localhost/library.properties "
) . as_dict ( )
assert data [ " name " ] == " library.properties "
2019-10-02 17:54:59 +03:00
def test_examples_from_dir ( tmpdir_factory ) :
package_dir = tmpdir_factory . mktemp ( " project " )
2019-10-03 14:55:04 +03:00
package_dir . join ( " library.json " ) . write (
' { " name " : " pkg " , " version " : " 1.0.0 " , " examples " : [ " examples/*/*.pde " ]} '
)
2019-10-02 17:54:59 +03:00
examples_dir = package_dir . mkdir ( " examples " )
# PlatformIO project #1
pio_dir = examples_dir . mkdir ( " PlatformIO " ) . mkdir ( " hello " )
2019-10-02 20:42:01 +03:00
pio_dir . join ( " .vimrc " ) . write ( " " )
2019-10-07 20:35:01 +03:00
pio_ini = pio_dir . join ( " platformio.ini " )
pio_ini . write ( " " )
2019-10-15 13:05:56 +03:00
if not WINDOWS :
pio_dir . join ( " platformio.ini.copy " ) . mksymlinkto ( pio_ini )
2019-10-02 17:54:59 +03:00
pio_dir . mkdir ( " include " ) . join ( " main.h " ) . write ( " " )
pio_dir . mkdir ( " src " ) . join ( " main.cpp " ) . write ( " " )
# wiring examples
2019-10-03 12:47:41 +03:00
arduino_dir = examples_dir . mkdir ( " 1. General " )
arduino_dir . mkdir ( " SomeSketchIno " ) . join ( " SomeSketchIno.ino " ) . write ( " " )
arduino_dir . mkdir ( " SomeSketchPde " ) . join ( " SomeSketchPde.pde " ) . write ( " " )
2019-10-02 17:54:59 +03:00
# custom examples
demo_dir = examples_dir . mkdir ( " demo " )
demo_dir . join ( " demo.cpp " ) . write ( " " )
demo_dir . join ( " demo.h " ) . write ( " " )
demo_dir . join ( " util.h " ) . write ( " " )
# PlatformIO project #2
pio_dir = examples_dir . mkdir ( " world " )
pio_dir . join ( " platformio.ini " ) . write ( " " )
pio_dir . join ( " README " ) . write ( " " )
pio_dir . join ( " extra.py " ) . write ( " " )
pio_dir . mkdir ( " include " ) . join ( " world.h " ) . write ( " " )
pio_dir . mkdir ( " src " ) . join ( " world.c " ) . write ( " " )
2019-10-03 12:47:41 +03:00
# example files in root
examples_dir . join ( " root.c " ) . write ( " " )
examples_dir . join ( " root.h " ) . write ( " " )
2019-10-02 17:54:59 +03:00
# invalid example
examples_dir . mkdir ( " invalid-example " ) . join ( " hello.json " )
# Do testing
2019-10-17 00:17:16 +03:00
raw_data = parser . ManifestParserFactory . new_from_dir ( str ( package_dir ) ) . as_dict ( )
assert isinstance ( raw_data [ " examples " ] , list )
assert len ( raw_data [ " examples " ] ) == 6
2019-10-02 17:54:59 +03:00
2019-10-18 22:00:28 +03:00
def _to_unix_path ( path ) :
return re . sub ( r " [ \\ /]+ " , " / " , path )
2019-10-02 17:54:59 +03:00
def _sort_examples ( items ) :
for i , item in enumerate ( items ) :
2019-10-18 22:00:28 +03:00
items [ i ] [ " base " ] = _to_unix_path ( items [ i ] [ " base " ] )
items [ i ] [ " files " ] = [ _to_unix_path ( f ) for f in sorted ( items [ i ] [ " files " ] ) ]
2019-10-02 17:54:59 +03:00
return sorted ( items , key = lambda item : item [ " name " ] )
2019-10-17 00:17:16 +03:00
raw_data [ " examples " ] = _sort_examples ( raw_data [ " examples " ] )
2019-12-29 13:13:04 +02:00
data = ManifestSchema ( ) . load_manifest ( raw_data )
2019-10-17 00:17:16 +03:00
assert not jsondiff . diff (
data ,
{
2019-10-02 17:54:59 +03:00
" version " : " 1.0.0 " ,
" name " : " pkg " ,
" examples " : _sort_examples (
[
{
" name " : " PlatformIO/hello " ,
2019-10-17 23:40:30 +03:00
" base " : os . path . join ( " examples " , " PlatformIO " , " hello " ) ,
" files " : [
" platformio.ini " ,
os . path . join ( " include " , " main.h " ) ,
os . path . join ( " src " , " main.cpp " ) ,
] ,
2019-10-02 17:54:59 +03:00
} ,
{
2019-10-03 12:47:41 +03:00
" name " : " 1_General/SomeSketchIno " ,
2019-10-17 23:40:30 +03:00
" base " : os . path . join ( " examples " , " 1. General " , " SomeSketchIno " ) ,
2019-10-02 17:54:59 +03:00
" files " : [ " SomeSketchIno.ino " ] ,
} ,
{
2019-10-03 12:47:41 +03:00
" name " : " 1_General/SomeSketchPde " ,
2019-10-17 23:40:30 +03:00
" base " : os . path . join ( " examples " , " 1. General " , " SomeSketchPde " ) ,
2019-10-02 17:54:59 +03:00
" files " : [ " SomeSketchPde.pde " ] ,
} ,
{
" name " : " demo " ,
2019-10-17 23:40:30 +03:00
" base " : os . path . join ( " examples " , " demo " ) ,
2019-10-02 17:54:59 +03:00
" files " : [ " demo.h " , " util.h " , " demo.cpp " ] ,
} ,
{
" name " : " world " ,
" base " : " examples/world " ,
" files " : [
" platformio.ini " ,
2019-10-17 23:40:30 +03:00
os . path . join ( " include " , " world.h " ) ,
os . path . join ( " src " , " world.c " ) ,
2019-10-02 17:54:59 +03:00
" README " ,
" extra.py " ,
] ,
} ,
2019-10-03 12:47:41 +03:00
{
" name " : " Examples " ,
" base " : " examples " ,
" files " : [ " root.c " , " root.h " ] ,
} ,
2019-10-02 17:54:59 +03:00
]
) ,
2019-10-17 00:17:16 +03:00
} ,
2019-10-04 18:30:48 +03:00
)
2020-06-15 22:05:59 +03:00
def test_parser_from_archive ( tmpdir_factory ) :
pkg_dir = tmpdir_factory . mktemp ( " package " )
pkg_dir . join ( " package.json " ) . write ( ' { " name " : " package.json " } ' )
pkg_dir . join ( " library.json " ) . write ( ' { " name " : " library.json " } ' )
pkg_dir . join ( " library.properties " ) . write ( " name=library.properties " )
archive_path = os . path . join ( str ( pkg_dir ) , " package.tar.gz " )
with tarfile . open ( archive_path , mode = " w|gz " ) as tf :
for item in os . listdir ( str ( pkg_dir ) ) :
tf . add ( os . path . join ( str ( pkg_dir ) , item ) , item )
data = parser . ManifestParserFactory . new_from_archive ( archive_path ) . as_dict ( )
assert data [ " name " ] == " library.json "
2019-10-17 12:38:35 +03:00
def test_broken_schemas ( ) :
2019-12-29 13:13:04 +02:00
# missing required field
with pytest . raises (
ManifestValidationError , match = ( " Invalid semantic versioning format " )
) as exc_info :
ManifestSchema ( ) . load_manifest ( dict ( name = " MyPackage " , version = " broken_version " ) )
assert exc_info . value . valid_data == { " name " : " MyPackage " }
2019-09-30 23:45:03 +03:00
2019-12-29 13:13:04 +02:00
# invalid StrictList
2019-10-17 00:17:16 +03:00
with pytest . raises (
2019-12-29 13:13:04 +02:00
ManifestValidationError , match = ( " Invalid manifest fields.+keywords " )
) as exc_info :
ManifestSchema ( ) . load_manifest (
dict ( name = " MyPackage " , version = " 1.0.0 " , keywords = [ " kw1 " , " *^[] " ] )
)
assert list ( exc_info . value . messages . keys ( ) ) == [ " keywords " ]
assert exc_info . value . valid_data [ " keywords " ] == [ " kw1 " ]
2019-09-30 23:45:03 +03:00
2019-10-01 16:13:25 +03:00
# broken SemVer
2019-09-30 23:45:03 +03:00
with pytest . raises (
2019-10-17 00:17:16 +03:00
ManifestValidationError , match = ( " Invalid semantic versioning format " )
2019-09-30 23:45:03 +03:00
) :
2019-12-29 13:13:04 +02:00
ManifestSchema ( ) . load_manifest ( dict ( name = " MyPackage " , version = " broken_version " ) )
2019-10-02 12:34:50 +03:00
2019-10-17 00:17:16 +03:00
# broken value for Nested
with pytest . raises ( ManifestValidationError , match = r " authors.*Invalid input type " ) :
2019-12-29 13:13:04 +02:00
ManifestSchema ( ) . load_manifest (
2019-10-17 00:17:16 +03:00
dict (
name = " MyPackage " ,
description = " MyDescription " ,
keywords = [ " a " , " b " ] ,
authors = [ " should be dict here " ] ,
version = " 1.2.3 " ,
)
2019-10-02 12:34:50 +03:00
)
2020-06-22 15:25:02 +03:00
# invalid package name
with pytest . raises ( ManifestValidationError , match = ( " are not allowed " ) ) :
ManifestSchema ( ) . load_manifest ( dict ( name = " C/C++ :library " , version = " 1.2.3 " ) )