Skip to content

Commit 6619a5d

Browse files
committed
Basic test of plugin including CI
1 parent 736d71b commit 6619a5d

File tree

5 files changed

+154
-3
lines changed

5 files changed

+154
-3
lines changed

.github/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "github-actions"
4+
directory: "/"
5+
schedule:
6+
interval: "weekly"

.github/workflows/test.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: test
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: '8 15 * * 0' # 8:15 every Monday
8+
9+
jobs:
10+
build_and_test_plugin:
11+
strategy:
12+
matrix:
13+
os: [ ubuntu-latest, macos-latest, windows-latest ]
14+
15+
name: ${{ matrix.os }}
16+
runs-on: ${{ matrix.os }}
17+
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
with:
22+
path: src
23+
24+
- name: Install dependencies
25+
run: pip install 'ncrystal-core>=3.9.86' "scikit-build-core>=0.10" "ncrystal-pypluginmgr>=0.0.3" "ncrystal-python>=3.9.86"
26+
27+
- name: Install plugin
28+
run: pip install ./src
29+
30+
- name: Verify plugin loading
31+
shell: python
32+
run: |
33+
import os
34+
os.environ['NCRYSTAL_PLUGIN_RUNTESTS'] = '1'
35+
os.environ['NCRYSTAL_REQUIRED_PLUGINS'] = 'SANSND'
36+
import NCrystal
37+
NCrystal.browsePlugins(dump=True)
38+
39+
- name: Use plugin file
40+
run: nctool -d 'plugins::SANSND/ncplugin-SANSND_nanodiamond.ncmat'
41+
42+
- name: Load all plugin files
43+
shell: python
44+
run: |
45+
from NCrystal.datasrc import browseFiles
46+
from NCrystal.core import createScatter
47+
for f in browseFiles(factory='plugins'):
48+
if f.name.startswith('SANSND/'):
49+
print('Loading f.fullKey')
50+
createScatter( f'{f.fullKey}' )

src/NCPluginBoilerPlate.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
#include "NCrystal/NCPluginBoilerplate.hh"
2828

2929
#include "NCPluginFactory.hh"
30-
//#include "NCTestPlugin.hh"
30+
#include "NCTestPlugin.hh"
3131

3232
void NCP::registerPlugin()
3333
{
3434
//This function is required for the plugin to work. It should register
3535
//factories, and potentially other stuff as appropriate for the plugin (like
3636
//adding in-mem data files, adding test functions, ...).
3737
NC::FactImpl::registerFactory(std::make_unique<NCP::PluginFactory>());
38-
// NC::Plugins::registerPluginTestFunction( std::string("test_") + pluginName(),
39-
// customPluginTest );
38+
NC::Plugins::registerPluginTestFunction( std::string("test_") + pluginName(),
39+
customPluginTest );
4040
};

src/NCTestPlugin.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
////////////////////////////////////////////////////////////////////////////////
3+
// //
4+
// This file is part of NCrystal (see https://mctools.github.io/ncrystal/) //
5+
// //
6+
// Copyright 2015-2024 NCrystal developers //
7+
// //
8+
// Licensed under the Apache License, Version 2.0 (the "License"); //
9+
// you may not use this file except in compliance with the License. //
10+
// You may obtain a copy of the License at //
11+
// //
12+
// http://www.apache.org/licenses/LICENSE-2.0 //
13+
// //
14+
// Unless required by applicable law or agreed to in writing, software //
15+
// distributed under the License is distributed on an "AS IS" BASIS, //
16+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
17+
// See the License for the specific language governing permissions and //
18+
// limitations under the License. //
19+
// //
20+
////////////////////////////////////////////////////////////////////////////////
21+
22+
#include "NCTestPlugin.hh"
23+
#include "NCrystal/internal/utils/NCMsg.hh"
24+
#include "NCrystal/dump/NCDump.hh"
25+
26+
void NCP::customPluginTest()
27+
{
28+
//This function is called by NCrystal after the plugin is loaded, but only if
29+
//the NCRYSTAL_PLUGIN_RUNTESTS environment variable is set to "1". In case of
30+
//errors or anything about the plugin not working, simply throw an exception
31+
//(which is what the nc_assert_always function does below, but feel free to
32+
//simply throw an exception directly).
33+
34+
NCRYSTAL_MSG("Testing plugin "<<pluginName());
35+
36+
//Create some test NCMAT data. For simplicity we will here base it on an
37+
//existing file, but add our @CUSTOMPLUGIN section (see NCPhysicsModel.cc for
38+
//a description of the format).:
39+
40+
//Make sure we can load:
41+
NCRYSTAL_MSG("Loading 'plugins::SANSND/ncplugin-SANSND_nanodiamond.ncmat'");
42+
NCRYSTAL_MSG(" -> info object:");
43+
auto info
44+
= NC::createInfo("plugins::SANSND/ncplugin-SANSND_nanodiamond.ncmat");
45+
NC::dump(info);
46+
47+
NCRYSTAL_MSG(" -> scatter process:");
48+
auto sc
49+
= NC::createScatter("plugins::SANSND/ncplugin-SANSND_nanodiamond.ncmat");
50+
NCRYSTAL_RAWOUT(sc.underlying().jsonDescription())
51+
52+
//Sanity check, some standard material does NOT have sans:
53+
nc_assert_always( NC::createScatter("stdlib::Al_sg225.ncmat"
54+
";comp=sans").isNull() );
55+
//But our nanodiamonds do:
56+
nc_assert_always( ! NC::createScatter("plugins::SANSND/ncplugin-SANSND_nanodiamond.ncmat"
57+
";comp=sans").isNull() );
58+
59+
60+
NCRYSTAL_MSG("All tests of plugin "<<pluginName()<<" were successful!");
61+
}

src/NCTestPlugin.hh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
////////////////////////////////////////////////////////////////////////////////
3+
// //
4+
// This file is part of NCrystal (see https://mctools.github.io/ncrystal/) //
5+
// //
6+
// Copyright 2015-2024 NCrystal developers //
7+
// //
8+
// Licensed under the Apache License, Version 2.0 (the "License"); //
9+
// you may not use this file except in compliance with the License. //
10+
// You may obtain a copy of the License at //
11+
// //
12+
// http://www.apache.org/licenses/LICENSE-2.0 //
13+
// //
14+
// Unless required by applicable law or agreed to in writing, software //
15+
// distributed under the License is distributed on an "AS IS" BASIS, //
16+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
17+
// See the License for the specific language governing permissions and //
18+
// limitations under the License. //
19+
// //
20+
////////////////////////////////////////////////////////////////////////////////
21+
22+
#ifndef NCPlugin_TestPlugin_hh
23+
#define NCPlugin_TestPlugin_hh
24+
25+
#include "NCrystal/NCPluginBoilerplate.hh"
26+
27+
namespace NCPluginNamespace {
28+
29+
//Function which can be called in order to test the plugin:
30+
void customPluginTest();
31+
32+
}
33+
34+
#endif

0 commit comments

Comments
 (0)