Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit a222c1c

Browse files
test PR webhook and move-psiotbuild script (#14)
* test PR webhook and move-psiotbuild script * add first real test * I2C tests * add gpio tests * add spi tests * misc feedback from Andrew * address Andrew's feedback * Update vsts.ps1 * upload artifacts and tests * add travis' vstsBuild module * bucket name and empty file * remove star
1 parent f56aadc commit a222c1c

13 files changed

+361
-49
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ matrix:
2424
- powershell
2525

2626
script:
27-
- pwsh -c 'Install-Module InvokeBuild -Force -Scope CurrentUser; Invoke-Build'
27+
- pwsh -c 'Install-Module InvokeBuild -Force -Scope CurrentUser; Invoke-Build'

Examples/Microsoft.PowerShell.IoT.BME280/Microsoft.PowerShell.IoT.BME280.psm1

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ function Get-BME280Device
1212
(
1313
[ValidateNotNullOrEmpty()]
1414
[int]
15-
$Id = 0x77
15+
$Id = 0x77,
16+
17+
[ValidateNotNullOrEmpty()]
18+
[string]
19+
$FriendlyName = "BME280"
1620
)
17-
$script:Device = Get-I2CDevice -Id $Id -FriendlyName BME280
21+
$script:Device = Get-I2CDevice -Id $Id -FriendlyName $FriendlyName
1822
return $script:Device
1923
}
2024

@@ -297,4 +301,4 @@ function Compensate_H
297301
if ($v_x1_u32r -gt 419430400) {$v_x1_u32r = 419430400}
298302
$v_x1_u32r = ($v_x1_u32r -shr 12)*100 / 1024
299303
$v_x1_u32r
300-
}
304+
}

Move-PSIoTBuild.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ end {
4545
}
4646
}
4747
}
48-
}
48+
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,4 @@ but they don't do anything right now:
136136
./build.ps1 -Test
137137
```
138138

139-
We will investigate standing up a test rig of supported devices and OS's so that we can test against actual hardware.
139+
We will investigate standing up a test rig of supported devices and OS's so that we can test against actual hardware.

appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ test_script:
2020

2121
artifacts:
2222
- path: out\Microsoft.PowerShell.IoT
23-
name: Microsoft.PowerShell.IoT
23+
name: Microsoft.PowerShell.IoT

build.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,4 @@ if ($Bootstrap) {
9090
if($Test) {
9191
Invoke-Build Test
9292
}
93-
}
93+
}

psiot.build.ps1

+8-3
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ task Build Restore, {
3131
task Test {
3232
Install-Module Pester -Force -Scope CurrentUser
3333
Push-Location $PSScriptRoot\test
34-
$res = Invoke-Pester -OutputFormat NUnitXml -OutputFile TestsResults.xml -PassThru;
34+
$res = Invoke-Pester -OutputFormat NUnitXml -OutputFile TestsResults.xml -PassThru
3535
if ($env:APPVEYOR) {
3636
(New-Object System.Net.WebClient).UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path .\TestsResults.xml));
3737
}
3838
if ($res.FailedCount -gt 0) { throw "$($res.FailedCount) tests failed."}
3939
Pop-Location
4040
}
4141

42-
task Package Build, {
42+
task Package Clean, Build, {
4343
if ((Test-Path "$PSScriptRoot\out")) {
4444
Remove-Item -Path $PSScriptRoot\out -Recurse -Force
4545
}
@@ -53,7 +53,12 @@ task Package Build, {
5353

5454
Copy-Item -Path "$PSScriptRoot\src\Microsoft.PowerShell.IoT\Microsoft.PowerShell.IoT.psd1" -Destination "$PSScriptRoot\out\Microsoft.PowerShell.IoT\" -Force
5555
Copy-Item -Path "$PSScriptRoot\src\Microsoft.PowerShell.IoT\bin\Debug\netcoreapp2.0\publish\*" -Destination "$PSScriptRoot\out\Microsoft.PowerShell.IoT\" -Force -Recurse
56+
57+
if ($env:TF_BUILD) {
58+
Import-Module "$PSScriptRoot\tools\vstsBuild.psm1"
59+
Publish-VstsBuildArtifact -ArtifactPath "$PSScriptRoot\out\Microsoft.PowerShell.IoT" -Bucket "Microsoft.PowerShell.IoT"
60+
}
5661
}
5762

5863
# The default task is to run the entire CI build
59-
task . Clean, Build, Test, Package
64+
task . Package

src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.cs

+31-31
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,21 @@ public GpioPinData(int id, SignalLevel value, Unosquare.RaspberryIO.Gpio.GpioPin
8383
this.Value = value;
8484
this.PinInfo = pinInfo;
8585
}
86-
}
87-
86+
}
87+
8888
public class SPIData
8989
{
9090
public uint Channel { get; set; }
9191
public uint Frequency { get; set; }
9292
public byte[] Data { get; set; }
93-
public byte[] Responce { get; set; }
93+
public byte[] Response { get; set; }
9494

95-
public SPIData(uint channel, uint frequency, byte[] data, byte[] responce)
95+
public SPIData(uint channel, uint frequency, byte[] data, byte[] response)
9696
{
9797
this.Channel = channel;
9898
this.Frequency = frequency;
9999
this.Data = data;
100-
this.Responce = responce;
100+
this.Response = response;
101101
}
102102
}
103103

@@ -308,35 +308,35 @@ public class SendSPIData : Cmdlet
308308
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
309309
public SwitchParameter Raw { get; set; }
310310

311-
public SendSPIData()
312-
{
313-
this.Channel = 0;
314-
this.Frequency = Unosquare.RaspberryIO.Gpio.SpiChannel.MinFrequency;
311+
public SendSPIData()
312+
{
313+
this.Channel = 0;
314+
this.Frequency = Unosquare.RaspberryIO.Gpio.SpiChannel.MinFrequency;
315315
}
316316

317317
protected override void ProcessRecord()
318-
{
319-
var spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel0;
320-
if (this.Channel == 1)
321-
{
322-
spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel1;
323-
Unosquare.RaspberryIO.Pi.Spi.Channel1Frequency = (int)this.Frequency;
324-
}
325-
else
326-
{
327-
Unosquare.RaspberryIO.Pi.Spi.Channel0Frequency = (int)this.Frequency;
328-
};
329-
330-
var responce = spiChannel.SendReceive(this.Data);
331-
if (this.Raw)
332-
{
333-
WriteObject(responce);
334-
}
335-
else
336-
{
337-
SPIData spiData = new SPIData(this.Channel, this.Frequency, this.Data, responce);
338-
WriteObject(spiData);
318+
{
319+
var spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel0;
320+
if (this.Channel == 1)
321+
{
322+
spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel1;
323+
Unosquare.RaspberryIO.Pi.Spi.Channel1Frequency = (int)this.Frequency;
324+
}
325+
else
326+
{
327+
Unosquare.RaspberryIO.Pi.Spi.Channel0Frequency = (int)this.Frequency;
328+
};
329+
330+
var response = spiChannel.SendReceive(this.Data);
331+
if (this.Raw)
332+
{
333+
WriteObject(response);
334+
}
335+
else
336+
{
337+
SPIData spiData = new SPIData(this.Channel, this.Frequency, this.Data, response);
338+
WriteObject(spiData);
339339
}
340340
}
341341
}
342-
}
342+
}

src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.psd1

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
AliasesToExport = @()
1616
NestedModules=@('Microsoft.PowerShell.IoT.dll')
1717
HelpInfoURI = 'https://go.microsoft.com/fwlink/?LinkId=393254'
18-
}
18+
}

test/psiot.Tests.ps1

+94-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,99 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4-
Describe "Empty Test" {
5-
Context "Empty Test" {
6-
It "Empty Test" {
7-
$true | Should Be $true
4+
Describe "PowerShell IoT tests" {
5+
BeforeAll {
6+
# This creates the session to the test Pi. The hostname maps to a random IP
7+
$Global:SESSION = New-PSSession -HostName raspberry -UserName pi
8+
}
9+
Context "I2C tests" {
10+
BeforeAll {
11+
Invoke-Command -Session $Global:SESSION -ScriptBlock {
12+
# Import the example BME280 which wraps PowerShell IoT cmdlets
13+
Import-Module Microsoft.PowerShell.IoT.BME280
14+
}
15+
16+
}
17+
It "Can get the the BME280 I2C device" {
18+
$device = Invoke-Command -Session $Global:SESSION -ScriptBlock {
19+
return Get-BME280Device -Id 0x76
20+
}
21+
$device | Should -Not -BeNullOrEmpty
22+
$device.Id | Should -Be 118
23+
$device.FriendlyName | Should -Be "BME280"
24+
}
25+
It "Can get the BME280 data" {
26+
$data = Invoke-Command -Session $Global:SESSION -ScriptBlock {
27+
return Get-BME280Data
28+
}
29+
$data.Temperature | Should -Not -BeNullOrEmpty
30+
$data.Pressure | Should -Not -BeNullOrEmpty
31+
$data.Humidity | Should -Not -BeNullOrEmpty
32+
}
33+
}
34+
Context "GPIO tests" {
35+
# GPIO pins can either act as an input or output pin. In other words,
36+
# you can either set a pin's value or read a pin's value. For example,
37+
# if you set pin 20 to "High" (aka 1) and attempt to read the value of
38+
# pin 20, you will not get the result of your previous set operation.
39+
40+
# To get around this limitation, on the test Raspberry Pi we have two pins
41+
# (22 and 26) connected. By doing this, we can set the value on pin 22 and
42+
# read that value on pin 26. This next test demonstrates that.
43+
It "Can get and set a GPIO's pin value" {
44+
$highValueResult = Invoke-Command -Session $Global:SESSION -ScriptBlock {
45+
Set-GpioPin -Id 26 -Value High
46+
return Get-GpioPin -Id 22
47+
}
48+
$highValueResult.Id | Should -Be 22
49+
$highValueResult.Value | Should -Be "High"
50+
51+
$lowValueResult = Invoke-Command -Session $Global:SESSION -ScriptBlock {
52+
Set-GpioPin -Id 26 -Value Low
53+
return Get-GpioPin -Id 22
54+
}
55+
$lowValueResult.Id | Should -Be 22
56+
$lowValueResult.Value | Should -Be "Low"
57+
}
58+
It "Can use the -Raw flag to get the raw value" {
59+
$rawValue = Invoke-Command -Session $Global:SESSION -ScriptBlock {
60+
Set-GpioPin -Id 26 -Value High
61+
return Get-GpioPin -Id 22 -Raw
62+
}
63+
$rawValue | Should -Be 1
64+
}
65+
It "Read non-connected pin with PullDown and return Low" {
66+
$result = Invoke-Command -Session $Global:SESSION -ScriptBlock {
67+
return Get-GpioPin -Id 23 -PullMode PullDown -Raw
68+
}
69+
$result | Should -Be 0
70+
}
71+
It "Read non-connected pin with PullUp and return High" {
72+
$result = Invoke-Command -Session $Global:SESSION -ScriptBlock {
73+
return Get-GpioPin -Id 23 -PullMode PullUp -Raw
74+
}
75+
$result | Should -Be 1
76+
}
77+
}
78+
Context "SPI tests" {
79+
# SPI test: LIS3DH motion sensor; datasheet: www.st.com/resource/en/datasheet/lis3dh.pdf
80+
# Read "WHO_AM_I (0Fh)" register; value should be 0x33
81+
It "Can read data from the LIS3DH motion sensor" {
82+
$result = Invoke-Command -Session $Global:SESSION -ScriptBlock {
83+
$d = @(0x8F,0x0)
84+
return Send-SPIData -Channel 0 -Data $d
85+
}
86+
$result.Channel | Should -Be 0
87+
$result.Data[0] | Should -Be 0x8F
88+
$result.Responce[1] | Should -Be 0x33
89+
$result.Frequency | Should -Be 500000
90+
}
91+
It "Can use the -Raw flag to get the raw value" {
92+
$result = Invoke-Command -Session $Global:SESSION -ScriptBlock {
93+
$d = @(0x8F,0x0)
94+
return Send-SPIData -Channel 0 -Data $d -Raw
95+
}
96+
$result[1] | Should -Be 0x33
897
}
998
}
10-
}
99+
}

tools/vsts.ps1

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Push-Location "$PSScriptRoot/../"
2+
3+
Install-Module InvokeBuild -Scope CurrentUser -Force
4+
5+
# Build and package for Pi
6+
Invoke-Build
7+
8+
# Move build to Pi
9+
./Move-PSIoTBuild.ps1 -Ip raspberry -WithExample Microsoft.PowerShell.IoT.BME280
10+
11+
# Run Pester tests
12+
Push-Location "test"
13+
Invoke-Build Test
14+
15+
Pop-Location
16+
Pop-Location

0 commit comments

Comments
 (0)